IOS-传输数据 - 一个DB到另一个DB-不同的加密密钥 - Sqlcipher

时间:2014-06-11 10:57:46

标签: ios sqlite sqlcipher

我的应用中有3个数据库(客户,客户端,产品)。我想将一个DB传输到另外两个DB。

三个DB被加密,那些加密KEYS是不同的。 如果我对三个数据库使用相同的密钥,它可以工作。但如果我使用了不同的密钥,则会返回错误代码26。

我使用下面的代码来附加数据库。请指导我。

// _database现在等于客户端。

 NSMutableString *tempString = [[NSMutableString alloc]initWithString:@"attach DATABASE  'customer' as c1 "];
  int resultCode = sqlite3_exec(_database, [tempString UTF8String], NULL, NULL, NULL);
    [tempString release]; tempString = nil;

    if (resultCode == SQLITE_OK)
    {

        tempString = [[NSMutableString alloc]initWithString:@"INSERT INTO table SELECT * FROM c1.table"];

        sqlite3_stmt *stmt_version = 0x00;
        resultCode = sqlite3_exec(_database, [tempString UTF8String], NULL, &stmt_version, NULL);
        [tempString release]; tempString = nil;
        sqlite3_finalize(stmt_version);
        if (resultCode == SQLITE_OK)
        {
            status =  YES;
        }
    }
   tempString = [[NSMutableString alloc]initWithString:@"DETACH DATABASE c1 "];
    sqlite3_exec(_database, [tempString UTF8String], NULL, NULL, NULL);
    [tempString release]; tempString = nil;

1 个答案:

答案 0 :(得分:0)

ATTACH命令也允许您提供密钥。此外,SQLCipher提供了一个便捷函数sqlcipher_export来复制数据库的模式和数据。下面是使用一个密钥创建数据库,然后使用不同的密钥将其数据和模式导出到另一个加密数据库的示例。

$> ./sqlcipher foo.db
sqlite> PRAGMA key = 'foo';
sqlite> CREATE table t1(a,b);
sqlite> INSERT INTO t1(a,b) values('one for the money', 'two for the show');
sqlite> ATTACH database 'bar.db' as bar KEY 'bar';
sqlite> SELECT sqlcipher_export('bar');
sqlite> DETACH database bar;
sqlite> .q
相关问题