Skip to content

sqlcipher_export - "table android_metadata already exists" #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Atom99 opened this issue May 18, 2012 · 11 comments
Closed

sqlcipher_export - "table android_metadata already exists" #55

Atom99 opened this issue May 18, 2012 · 11 comments

Comments

@Atom99
Copy link

Atom99 commented May 18, 2012

Hi,

I'm testing the v2.0.0 RC5 version.

I'm trying to export files from an unencrypted database to an encrypted database.
I'm using sqlcipher_export() function to export it.
Whenever i call sqlcipher_export() the error "table android_metadata already exists" occurs.
The android_metadata table is a default table created automatically. Of course, when I delete android_metadata table in the target database before calling sqlcipher_export() it works. But it would be nice that SqlCipher for Android would handle this.

Or maybe I'm doing somthing wrong?

[source]
SQLiteDatabase database = SQLiteDatabase.openDatabase(unEncryptedDBPath, "", null, SQLiteDatabase.OPEN_READWRITE);
database.execSQL("ATTACH DATABASE '" + encryptedDBPath + "' AS encrypted KEY 'testit';");
database.rawExecSQL("SELECT sqlcipher_export('encrypted');");
database.execSQL("DETACH DATABASE encrypted;");
database.close();
[/source]

regards,
Tom

@developernotes
Copy link
Member

Hi Atom99,

Since you are using the release candidate libraries, could you try the SQLiteDatabase. upgradeDatabaseFormatFromVersion1To2 function, just passing an empty string for your password and let us know your results? If the upgrade is successful, you could then issue a PRAGMA rekey = 'YourPassword'. Thanks!

@Atom99
Copy link
Author

Atom99 commented May 21, 2012

Hi,

SQLiteDatabase. upgradeDatabaseFormatFromVersion1To2 did not throw an exception, but rekey did not work - database is still unencrypted. here my sample code:

File databaseFile = getDatabasePath("unencrypted.db");
databaseFile.mkdirs();
databaseFile.delete();

android.database.sqlite.SQLiteDatabase databaseAndroid =
android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
databaseAndroid.execSQL("create table t1(a, b)");
databaseAndroid.execSQL("insert into t1(a, b) values(?, ?)",
new Object[] { "one for the money", "two for the show" });
databaseAndroid.close();

net.sqlcipher.database.SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(databaseFile, "");
net.sqlcipher.database.SQLiteDatabase databaseCipher =
net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(databaseFile, "", null);
databaseCipher.execSQL("PRAGMA rekey = 'testit'");
databaseCipher.close();

databaseCipher = net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(databaseFile, "testit", null);
Cursor c = databaseCipher.query("t1", new String[] { "b" }, null, null, null, null, null);
while (c.moveToNext())
{
Log.w(TAG, c.getString(0));
}
c.close();
databaseCipher.close();

net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(databaseFile, "testit", null); causes following error:

CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2099)
at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1965)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:898)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:926)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:933)

Is rekey pragma really the correct way to do that? I had a look on the SQLCipher API Doc and here it says: "PRAGMA rekey can not be used to encrypted a standard SQLite database! It is only useful for changing the key on an existing database. Instead, use sqlcipher_export() to encrypt a plaintext database." (http://sqlcipher.net/sqlcipher-api/#rekey)

@developernotes
Copy link
Member

Hi Atom99,

My apologizes for a delayed response. You are correct, rekey should only be used to change an existing password. I have put together an additional test that show the import of an unencrypted database to an encrypted database, the source can be found here [1].

  1. https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/src/main/java/net/zetetic/tests/ImportUnencryptedDatabaseTest.java

@Atom99 Atom99 closed this as completed May 24, 2012
@Atom99
Copy link
Author

Atom99 commented May 24, 2012

Hi,

Yes, that's how it works.
Thank you!

@developernotes
Copy link
Member

Hi Atom99,

Glad to hear you've got positive results. Thanks!

@girisharora28
Copy link

I am encrypting a database using sqlite expert & then i am importing it into my project. It gives me following error CREATE TABLE android_metadata failed , Failed to setLocale() when constructing, closing the database. But when i am importing database without encrypting it works fine. What should i do to work with encrypted database

@developernotes
Copy link
Member

Hi girisharora28,

I'm not aware that the SQLite Expert tool is compatible with the type of database encryption that occurs with SQLCipher. You may wish to contact the vendor to confirm. You could take an unencrypted SQLite database and encrypt it using sqlcipher_export, specifically example # 1.

@girisharora28
Copy link

Thanks for the reply. I think that it is not compatible otherwise it will open it, i want to know that how can i achieve this task. Simply i just have to make an encrypt database & import it into android application

@developernotes
Copy link
Member

Hi girisharora28,

Following the steps in example # 1 for sqlcipher_export will provide you with the encrypted database from a plaintext database. We have written an article on integrating SQLCipher for Android into an application available here.

@girisharora28
Copy link

Thanks once again..
Whai i am looking for is you had already provide in sqlcipher-android-tests / src / main / java / net / zetetic.
I have one simple sqlite database & want to encrypt it, in the above example you had provide it but as i am new to android & i am not able to understand exactly what is going on means i am not able to understand it. I f you have any other project related to it then please provide me its link

@developernotes
Copy link
Member

Hi girisharora28,

We have a mailing list which provides a great searchable resource of information when it comes to SQLCipher usage. Should you have specific questions about SQLCipher, I urge you to check it out. With regard to learning Android itself, there are many great resources available, including the training section within the Android site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants