0

I have 3 databases(customer,client,product) in my app. I want to transfer one DB to other two DB.

Three DB are encrypted and those encryption KEYS are different. If I have use the same key for three DB, It works. But If i have use different key's , it returns error code 26.

I am using below code for attaching the Databases. Please guide me.

// _database is equal to client now.

 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;
Finder
  • 8,259
  • 8
  • 39
  • 54

1 Answers1

0

The ATTACH command allows you to provide a key as well. Also, SQLCipher provides a convenience function sqlcipher_export to duplicate the schema and data of the database. Below is an example of creating a database with one key, then export its data and schema to another encrypted database with a different key.

$> ./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
Nick Parker
  • 1,378
  • 1
  • 7
  • 10
  • Thanks Parker. But I wants to transfer some specified tables. Not entire DB to another DB. In that case how will i do? – Finder Jun 12 '14 at 15:07
  • In that case you will need to replicate the portion of your schema that you need and perform the `INSERT INTO SELECT...` commands as needed. You can still use the `KEY` portion in your `ATTACH` statement to adjust the key on the other databases. – Nick Parker Jun 12 '14 at 16:07