【问题标题】:IOS-TranferData - one DB to another DB- different encryption keys - SqlicipherIOS-传输数据-一个数据库到另一个数据库-不同的加密密钥-Sqlcipher
【发布时间】:2014-06-11 10:57:46
【问题描述】:

我的应用中有 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;

【问题讨论】:

    标签: ios sqlite sqlcipher


    【解决方案1】:

    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
    

    【讨论】:

    • 谢谢帕克。但我想转移一些指定的表。不是整个数据库到另一个数据库。那我该怎么办?
    • 在这种情况下,您需要复制您需要的架构部分并根据需要执行INSERT INTO SELECT... 命令。您仍然可以使用ATTACH 语句中的KEY 部分来调整其他数据库上的键。
    猜你喜欢
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多