【问题标题】:Migrating SQLCipher ver. 2.x DB to ver. 3.x using by FMDB迁移 SQLCipher 版本。 2.x 数据库到版本。 FMDB 使用的 3.x
【发布时间】:2014-11-20 09:55:48
【问题描述】:

我看到了以下页面,我知道我应该执行查询“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;
}

【问题讨论】:

    标签: ios objective-c xcode fmdb sqlcipher


    【解决方案1】:

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

    如果你使用

    PRAGMA kdf_iter = 4000
    

    你不需要使用

    PRAGMA cipher_migrate
    

    【讨论】:

    • 很抱歉,但似乎无法正常工作。我按照您的回答编辑了我的代码,但它返回了一个错误,“DB 错误:26”文件已加密或不是数据库。”顺便说一句,旧版本是 2.1.1,新版本是 3.2.0 用于迁移。 ——
    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多