我看到了以下页面,并且我知道我应该执行查询“ PRAGMA cipher_migrate”。

https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_migrate

但是我不知道如何在FMDatabase上使用它。以下代码不起作用...
(似乎不仅执行时间错了...)

如果您尝试使用FMDatabase将SQLCipher ver.2.x DB迁移到ver.3.x,请让我知道您的解决方法。

- (FMDatabase *)openDB {

    NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directory = [directories objectAtIndex:0];
    NSString *path = [directory stringByAppendingPathComponent:dbFileName];
    FMDatabase *dataBase = [FMDatabase databaseWithPath:path];

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    int version = [userDefault integerForKey:DATABASE_TABLE_VERSION];

    if(version==1){

        if(![dataBase openWithFlags:SQLITE_OPEN_READWRITE]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"openWithFlags"
                                         userInfo:nil];
        }

        if(![dataBase setKey:SQLCIPHER_KEY]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"setKey"
                                         userInfo:nil];
        }

        if(![dataBase executeStatements:@"PRAGMA kdf_iter = 4000"]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"executeStatements:kdf_iter"
                                         userInfo:nil];
        }

        [userDefault setInteger:2 forKey:DATABASE_TABLE_VERSION];
        [userDefault synchronize];

        return dataBase;
    }

    if(![dataBase open]){
        @throw [NSException exceptionWithName:@"DBException"
                                       reason:@"open"
                                     userInfo:nil];
    }

    if(![dataBase setKey:SQLCIPHER_KEY]){
        @throw [NSException exceptionWithName:@"DBException"
                                       reason:@"setKey"
                                     userInfo:nil];
    }

    return dataBase;
}

- (NSMutableArray *)selectUser{

    FMDatabase *dataBase = [self openDB];
    NSString *sql = @"select * from t_user";
    FMResultSet *resultSet = [dataBase executeQuery:sql];

    NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects: nil];

    while ([resultSet next]){
        [mutableArray addObject:[resultSet resultDictionary]];
    }

    [resultSet close];
    [dataBase close];
    return mutableArray;
}

最佳答案

我认为您需要在连接之后和任何执行之前设置密码密钥。

另外,如果您使用

PRAGMA kdf_iter = 4000


你不需要用

PRAGMA cipher_migrate

关于ios - 迁移SQLCipher版本。 2.x DB到ver。 FMDB使用的3.x,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27036546/

10-10 21:33