将未加密的表移动到加密的sqlite数据库

时间:2014-07-11 05:13:50

标签: ios sqlcipher

我想知道如何将未加密的sqlite表转移到加密的sqlite数据库。我使用sqlcipher加密数据库。

我已经尝试过这段代码,但它没有用。

NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent: @"contacts_enc.sqlite"];


sqlite3 *db;
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {

    const char* key = [@"1234" UTF8String];
    sqlite3_key(db, key, strlen(key));

    if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master", NULL, NULL, NULL) == SQLITE_OK) {
        NSLog(@"password is correct, or, database has been initialized") ;

        //START

        sqlite3_exec(db, "ATTACH DATABASE 'contacts.sqlite' AS encrypted KEY '1234';", NULL, NULL, NULL);
        sqlite3_exec(db, "CREATE TABLE encrypted.account(_id,username,password);", NULL, NULL, NULL);
        sqlite3_exec(db, "INSERT INTO encrypted.account SELECT * FROM account;", NULL, NULL, NULL);
        sqlite3_exec(db, "DETACH DATABASE encrypted;", NULL, NULL, NULL);

        //END
    }
}

1 个答案:

答案 0 :(得分:0)

SQLCipher提供了一个称为sqlcipher_export(…)的便捷函数,它将数据库的内容复制到另一个附加的数据库。对于您的方案,请查看文档中的示例1 here