net.sqlcipher.database.SQLiteException: error code 14: Could not open database

I am trying to decrypt .db file using sqlcipher.
Here is my code

Process suProcess=Runtime.getRuntime().exec(“su”);
DataOutputStream dos=new DataOutputStream(suProcess.getOutputStream());
dos.writeBytes(“adb shell” + “\n”);
dos.flush();
dos.writeBytes("cp “+dbPath+” "+ “/data/data/source.db\n”);
dos.flush();
// SystemClock.sleep(5000);
db=SQLiteDatabase.openOrCreateDatabase(destination,this.dbKey,null,hook);

I am executing this code on every second. For first few time the db decrypted perfectly but after sometime its giving me error below when I try to open db file:

08-21 20:05:08.011 30119-30119/? E/memtrack: Couldn’t load memtrack module (No such file or directory)
08-21 20:05:08.011 30119-30119/? E/android.os.Debug: failed to load memtrack module: -2
08-21 20:05:08.032 30137-30137/? D/su: su invoked.
08-21 20:05:08.032 30137-30137/? D/su: starting daemon client 10026 10026
08-21 20:05:08.057 30141-30141/? D/su: remote pid: 30137
08-21 20:05:08.059 30141-30141/? D/su: remote pts_slave:
08-21 20:05:08.061 30141-30141/? D/su: waiting for child exit
08-21 20:05:08.102 30143-30143/? D/su: su invoked.
08-21 20:05:08.110 30143-30143/? D/su: db allowed
08-21 20:05:08.110 30143-30143/? D/su: 10026 /system/xbin/su executing 0 /system/bin/sh using binary /system/bin/sh : sh
08-21 20:05:08.135 30119-30119/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
08-21 20:05:08.184 30119-30136/? I/dalvikvm: Total arena pages for JIT: 11
08-21 20:05:08.215 30119-30136/? I/dalvikvm: Total arena pages for JIT: 12
08-21 20:05:08.216 30119-30136/? I/dalvikvm: Total arena pages for JIT: 13
08-21 20:05:08.225 30119-30136/? I/dalvikvm: Total arena pages for JIT: 14
08-21 20:05:08.231 30119-30136/? I/dalvikvm: Total arena pages for JIT: 15
08-21 20:05:08.233 30119-30136/? I/dalvikvm: Total arena pages for JIT: 16
08-21 20:05:08.236 30119-30136/? I/dalvikvm: Total arena pages for JIT: 17
08-21 20:05:08.240 30119-30136/? I/dalvikvm: Total arena pages for JIT: 18
08-21 20:05:08.247 30119-30136/? I/dalvikvm: Total arena pages for JIT: 19
08-21 20:05:08.255 30119-30136/? I/dalvikvm: Total arena pages for JIT: 20
08-21 20:05:08.370 30119-30119/? D/AndroidRuntime: Shutting down VM
08-21 20:05:08.371 30119-30135/? D/jdwp: Got wake-up signal, bailing out of select
08-21 20:05:08.371 30119-30135/? D/dalvikvm: Debugger has detached; object registry had 1 entries
08-21 20:05:08.376 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.378 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.380 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.384 30119-30136/? D/dalvikvm: Compiler shutdown in progress - discarding request
08-21 20:05:08.452 30144-30144/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-21 20:05:08.452 30144-30144/? D/AndroidRuntime: CheckJNI is OFF
08-21 20:05:08.455 30144-30144/? I/dalvikvm: No library specified. The standard exception handler will be used
08-21 20:05:08.476 30144-30144/? D/dalvikvm: Trying to load lib libjavacore.so 0x0
08-21 20:05:08.477 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.481 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.481 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.482 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.482 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.483 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.484 30144-30144/? D/dalvikvm: Added shared lib libjavacore.so 0x0
08-21 20:05:08.492 30144-30144/? D/dalvikvm: Trying to load lib libnativehelper.so 0x0
08-21 20:05:08.492 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.492 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.493 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.493 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.493 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.494 30144-30144/? E/houdini_hook: Unable to open libdvm_houdini lib
08-21 20:05:08.494 30144-30144/? D/dalvikvm: Added shared lib libnativehelper.so 0x0
08-21 20:05:08.497 30144-30144/? D/dalvikvm: No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
08-21 20:05:08.533 30144-30144/? D/dalvikvm: Extra Options: not specified
08-21 20:05:08.574 30144-30144/? D/dalvikvm: Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
08-21 20:05:08.848 30144-30144/? E/memtrack: Couldn’t load memtrack module (No such file or directory)
08-21 20:05:08.849 30144-30144/? E/android.os.Debug: failed to load memtrack module: -2
08-21 20:05:09.002 30144-30167/? I/dalvikvm: Total arena pages for JIT: 11
08-21 20:05:09.003 30144-30167/? I/dalvikvm: Total arena pages for JIT: 12
08-21 20:05:09.005 30144-30167/? I/dalvikvm: Total arena pages for JIT: 13
08-21 20:05:09.008 30144-30167/? I/dalvikvm: Total arena pages for JIT: 14
08-21 20:05:09.008 30144-30144/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
08-21 20:05:09.015 30144-30167/? I/dalvikvm: Total arena pages for JIT: 15
08-21 20:05:09.015 30144-30167/? I/dalvikvm: Total arena pages for JIT: 16
08-21 20:05:09.019 30144-30167/? I/dalvikvm: Total arena pages for JIT: 17
08-21 20:05:09.025 30144-30167/? I/dalvikvm: Total arena pages for JIT: 18
08-21 20:05:09.026 30144-30167/? I/dalvikvm: Total arena pages for JIT: 19
08-21 20:05:09.028 30144-30167/? I/dalvikvm: Total arena pages for JIT: 20
08-21 20:05:09.102 30144-30144/? D/AndroidRuntime: Shutting down VM
08-21 20:05:09.103 30144-30166/? D/jdwp: Got wake-up signal, bailing out of select
08-21 20:05:09.103 30144-30166/? D/dalvikvm: Debugger has detached; object registry had 1 entries
08-21 20:05:12.975 4554-4679/ W/System.err: net.sqlcipher.database.SQLiteException: error code 14: Could not open database
I have also tried to put delay of 5 to 10 seconds to make sure that its not because of incomplete copy operation of db file. But no success.

Hi @nilay_singh

It appears you are overwriting the file stored in another location via an external application. Doing so would not be considered a safe operation as we do not know the state of the database when you attempt to copy it. If you can provide a repeatable example within the SQLCipher for Android test suite we would be happy to look into this further.

state of db is normal as it is under my application means all write permissions are there sometimes it work and all of a sudden it stops when you say Repeatable Example I did not get you

@hsvmks - It would not be a safe operation to overwrite a SQLCipher database while it is in use. Furthermore, the scenario you mentioned is using an external process to manipulate the data. This isn’t really a supported scenario.

With regard to a repeatable example, we mean that you provide a complete standalone example that reliably demonstrates the problem in a test case, and then submit a pull request on GitHub.

That said, based on what we’re seeing here it doesn’t seem like a problem with SQLCipher itself.

net.sqlcipher.database.SQLiteException: error code 14: Could not open database this error are you sure it has nothing to do with SQLCipher I tried many thing copy db and then write permission and other things are correct means all open it just stops sometime

After debugging more I am seeing this error : calling error handler corrupt database detected .

And on this https://github.com/sqlcipher/android-database-sqlcipher/blob/master/src/net/sqlcipher/database/SQLiteDatabase.java

line number 537 I can see Calling error handler for corrupt database (detected) I want to know when and how this happens and what would be the best way to avoid this.

Hi @hsvmks

The call to onCorruption() within the Java layer is typically due to catching the SQLiteDatabaseCorruptionException. An example of where this is called can be found within the JNI layer when the PRAGMA integrity_check(1); command fails. As @sjlombardo mentioned above, you are attempting an unsupported scenario where the state of the database is not known, thus it would not be considered safe. It is difficult to speculate the cause without a repeatable example.

@hsvmks If we understand your process you are essentially overwriting the database every few seconds. SQLCipher is not a simple “flat file”, and opening a database does not decrypt it in its entirety. It is a complex file format that includes state information, version data, etc. You can’t just overwrite the file periodically. In effect you are probably corrupting the database every time you copy it like that, hence the corruption exception.