【发布时间】:2015-11-02 09:03:32
【问题描述】:
当我使用sqlcipher加密我的数据库,并在FMDatabaseQueue中调用inDatabase——成功!
但是当我将inDatabase 更改为inTransaction 时,控制台显示“文件已加密或不是数据库”。
代码:
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:st_dbPath];
// success
[queue inDatabase:^(FMDatabase *db) {
[db setKey:st_dbKey];
[db executeUpdate:@"INSERT INTO t_user VALUES (16)"];
}];
// fail : File is encrypted or is not a database
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db setKey:st_dbKey];
[db executeUpdate:@"INSERT INTO t_user VALUES (17)"];
}];
以及加密数据库代码:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *ecDB = [documentDir stringByAppendingPathComponent:st_dbEncryptedName];
// SQL Query. NOTE THAT DATABASE IS THE FULL PATH NOT ONLY THE NAME
const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '%@';", ecDB, st_dbKey] UTF8String];
sqlite3 *unencrypted_DB;
if (sqlite3_open([st_dbPath UTF8String], &unencrypted_DB) == SQLITE_OK) {
// Attach empty encrypted database to unencrypted database
sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, NULL);
// export database
sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL);
// Detach encrypted database
sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
sqlite3_close(unencrypted_DB);
}
else {
sqlite3_close(unencrypted_DB);
NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
}
加密代码来自那里:http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/
【问题讨论】:
标签: ios objective-c encryption fmdb