【发布时间】: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