Export() method returns metadata already exists

Trying to copy an encrypted database to a new unencrypted database

SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
    	            public void preKey(SQLiteDatabase sqLiteDatabase) {
    	                sqLiteDatabase.rawExecSQL("PRAGMA cipher_default_use_hmac = off;");
    	            }
    	            public void postKey(SQLiteDatabase sqLiteDatabase) {}
    	        };
    			
SQLiteDatabase myDb = SQLiteDatabase.openDatabase("/sdcard/bookscipher.db",    
                                                      "new_password", null,1);
			
SQLiteDatabase.openOrCreateDatabase("/sdcard/plaintext.db", "", null,hook);
			
myDb.execSQL("PRAGMA key = 'new_password';");
myDb.execSQL("ATTACH DATABASE '/sdcard/plaintext.db' AS plaintext KEY '';");
**myDb.execSQL("SELECT sqlcipher_export('plaintext');");**
myDb.execSQL("DETACH DATABASE plaintext;");

sqlite returned: error code = 1, msg = statement aborts at 2: [SELECT sqlcipher_export(‘plaintext’);] table android_metadata already exists

Hello @AVD

Does /sdcard/plaintext.db already exist? If so, you may wish to delete the file before your export.

yes there was no plaintext.db in /sdcard before I run the program. Still the error persists.

But on removing below line, it worked flawless

SQLiteDatabase.openOrCreateDatabase("/sdcard/plaintext.db", "", null,hook);

Also i had to use rawExecSql instead of execSql

Got help from https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/src/main/java/net/zetetic/tests/ExportToUnencryptedDatabase.java

So to know, ATTACH command will create the database ?

Thanks

Hello @AVD

We are glad to hear everything is working well for you now. The ATTACH command will create the database file for you if the file is not present.

1 Like