迁移 SQLCipher 版本.2.x DB 到版本.3.x 由 FMDB 使用 [英] Migrating SQLCipher ver. 2.x DB to ver. 3.x using by FMDB

查看:31
本文介绍了迁移 SQLCipher 版本.2.x DB 到版本.3.x 由 FMDB 使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了以下页面,我知道我应该执行查询PRAGMA cipher_migrate".

I saw the following page and I understand I should execute the query "PRAGMA cipher_migrate".

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

但我不知道如何在 FMDatabase 上使用它.以下代码不起作用...(好像不仅执行的时机不对……)

But I have no idea how to use it on FMDatabase. The following code did not work... (Seems like not only the timing of the execution is wrong...)

如果您尝试过使用 FMDatabase 将 SQLCipher ver.2.x DB 迁移到 ver.3.x,我希望您告诉我您的解决方法.

I'd like you to let me know your workaround if you have tried migrating SQLCipher ver.2.x DB to ver.3.x using by FMDatabase.

- (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;
}

推荐答案

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

I think you need to set cipher key right after connection and before any execution.

如果你使用

PRAGMA kdf_iter = 4000

你不需要使用

PRAGMA cipher_migrate

这篇关于迁移 SQLCipher 版本.2.x DB 到版本.3.x 由 FMDB 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆