我正在尝试将未加密的 sqlite3 数据库的内容添加到使用 SQLCipher 的加密数据库中。我已经基于我正在尝试做的事情this和this。然而,一些事情对我来说仍然不清楚。
顺带
ATTACH DATABASE
一提,加密数据库是否必须是类型.db
?可以.sqlite
匹配我的原始数据库吗?所述加密数据库是否必须已经存在?如果是这样,它应该在应用程序中的什么位置?我是否必须提供文件的路径(文档目录等)?
我在哪里可以找到成功加密的数据库?它将被保存在哪里?
这是我的代码:
+ (void)encryptDB
{
sqlite3 *unencrypted_DB;
NSString *path_u = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"dict.sqlite"];
if (sqlite3_open([path_u UTF8String], &unencrypted_DB) == SQLITE_OK) {
NSLog(@"Database Opened");
// Attach empty encrypted database to unencrypted database
sqlite3_exec(unencrypted_DB, "ATTACH DATABASE 'dict_encrypted.sqlite' AS encrypted KEY '1234';", NULL, NULL, NULL);
// Create new tables within encrypted database to match those in unencrypted database
sqlite3_exec(unencrypted_DB, "CREATE TABLE encrypted.t1(A,B,C);", NULL, NULL, NULL);
// Copy items from unencrypted database into encrypted database
sqlite3_exec(unencrypted_DB, "INSERT INTO encrypted.t1 SELECT * FROM t1;", NULL, NULL, NULL);
// Detach encrypted database
sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
NSLog (@"End database copying");
sqlite3_close(unencrypted_DB);
}
else {
sqlite3_close(unencrypted_DB);
NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
}
}