# Not able to open db created by android-database-sqlciphe 3.5.7 with new sqlcipher-android 4.9.0

**URL:** <https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080>\
**Category:** SQLCipher\
**Created:** [September 23, 2025, 10:56am UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080 "2025-09-23T10:56:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![erraisahmad](https://avatars.discourse-cdn.com/v4/letter/e/bbe5ce/32.png) [@erraisahmad](https://discuss.zetetic.net/u/erraisahmad)\
**Post date:** [September 23, 2025, 10:56am UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/1 "2025-09-23T10:56:11Z")

</div>

Hi,

We had been using the android-database-sqlcipher 3.5.7 in our project to secure the db.

In order to support the 16Kb pagesize, we decided to move to sqlcipher-android 4.9.0.

We are facing issues in upgrade scenarios, when the app is trying to open the existing database, which was created using v3.5.7.

We are getting following error

Failed to open database ‘/data/user/0/com.konylabs.android/databases/TEST\_DB\_ENCRYPTED’.  
android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(\*) FROM sqlite\_schema;  
at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)  
at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)  
at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)  
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)  
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)  
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)  
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)  
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)  
at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)  
at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)  
at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)  
at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:812)  
at net.zetetic.database.sqlcipher.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:929)

SQLiteDatabaseHook implementation in 3.5.2

```auto
public void preKey(SQLiteDatabase paramSQLiteDatabase){
    paramSQLiteDatabase.rawExecSQL("PRAGMA cipher_default_kdf_iter = 4000");
}

public void postKey(SQLiteDatabase paramSQLiteDatabase){
    
}

```

We are using following code in th SQLiteDatabaseHook implementation in 4.9.0

```auto
public void preKey(SQLiteConnection sqLiteConnection) {
		try {
			sqLiteConnection.executeForLong("PRAGMA cipher_default_kdf_iter = 4000;", null, null);
		} catch (Exception exception) {
			exception.printStackTrace();
			}
		}
		
	}
	public void postKey(SQLiteConnection sqLiteConnection) {
		try {
			sqLiteConnection.execute("PRAGMA cipher_compatibility = 3;", null, null);
		} catch (Exception exception) {
			exception.printStackTrace();
			}
		}
	}`

```

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 23, 2025, 1:17pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/2 "2025-09-23T13:17:59Z")

</div>

Hi @erraisahmad,

Try changing your `preKey` method to invoke `execute` instead of `executeForLong`. This works for me when querying a SQLCipher v3 database file from `sqlcipher-android`:

```kotlin
fun runSQLCipher(context: Context){
    val filename = "foo.db"
    val dbPath = context.getDatabasePath(filename)
    if(!dbPath.exists()){
      val inputStream = context.assets.open(filename)
      val outputStream = FileOutputStream(dbPath)
        val buffer = ByteArray(1024)
        var length: Int
        while ((inputStream.read(buffer).also { length = it }) > 0) {
            outputStream.write(buffer, 0, length)
        }
        outputStream.flush()
        outputStream.close()
        inputStream.close()
    }
    System.loadLibrary("sqlcipher")
    val hook: SQLiteDatabaseHook = object : SQLiteDatabaseHook {
        override fun preKey(db: SQLiteConnection?) {
            db?.execute("PRAGMA cipher_default_kdf_iter = 4000;", null, null)
        }
        override fun postKey(db: SQLiteConnection?) {
            db?.execute("PRAGMA cipher_compatibility = 3;", null, null)
        }
    }
    val db = SQLiteDatabase.openOrCreateDatabase(dbPath, "foo", null, null, hook)
    val cursor = db?.query("SELECT * FROM t1;")
    while(cursor != null && cursor.moveToNext()){
        val a = cursor.getString(0)
        val b = cursor.getString(1)
        Log.i("SQLCIPHER", "a = $a, b = $b")
    }
    cursor?.close()
    db?.close()
}

```

---

<div class="post-metadata">

**Author:** ![erraisahmad](https://avatars.discourse-cdn.com/v4/letter/e/bbe5ce/32.png) [@erraisahmad](https://discuss.zetetic.net/u/erraisahmad)\
**Post date:** [September 30, 2025, 6:37am UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/3 "2025-09-30T06:37:09Z")

</div>

Thank you for your reply.

The recommendation worked with a small change. It worked with combination of _PRAGMA cipher\_migrate_.

```auto
public void postKey(SQLiteConnection sqLiteConnection) {
		try {
			sqLiteConnection.execute("PRAGMA cipher_migrate;", null, null);
			sqLiteConnection.execute("PRAGMA cipher_compatibility = 3;", null, null);
		} catch (Exception exception) {
			if(DEBUG) {
				Log.d(TAG, " postKey PRAGMA statement execution Failed ");
				exception.printStackTrace();
			}
		}
	}

```

Thank you.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 30, 2025, 12:31pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/4 "2025-09-30T12:31:20Z")

</div>

Hi @erraisahmad,

Running `PRAGMA cipher_migrate` will perform an in-place upgrade of the entire database to the latest file format. As such, you do not need to execute `PRAGMA cipher_compatibility = 3` at all, you should remove that line.

---

<div class="post-metadata">

**Author:** ![erraisahmad](https://avatars.discourse-cdn.com/v4/letter/e/bbe5ce/32.png) [@erraisahmad](https://discuss.zetetic.net/u/erraisahmad)\
**Post date:** [October 9, 2025, 2:21pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/5 "2025-10-09T14:21:28Z")

</div>

Thank you @developernotes It really helped.

I am able to acheive the functionality.

I am seeing below exception in postkey method

`android.database.sqlite.SQLiteException: unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only.`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnection.nativeExecute(Native Method)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnection.execute(SQLiteConnection.java:592)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at com.konylabs.api.db.sqlcipher.KonySQLDatabase.postKey(KonySQLDatabase.java:207)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:237)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:474)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:188)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:180)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)`  
`2025-10-09 19:43:07.591 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)`  
`2025-10-09 19:43:07.593 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)`  
`2025-10-09 19:43:07.593 4201-4244 System.err com.konylabs.android W at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:740)`  
`2025-10-09 19:43:07.593 4201-4244 System.err com.konylabs.android W at com.konylabs.api.db.sqlcipher.KonySQLDatabase.openDatabase(KonySQLDatabase.java:111)`

The error message explicitly tells us to use SQLiteDatabase methods whereas the postkey method argument is SQLIteConnection.

Can you please help me reagarding this exception

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 9, 2025, 7:29pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/6 "2025-10-09T19:29:25Z")

</div>

What SQL command are you trying to run in the postKey event that causes the exception?

---

<div class="post-metadata">

**Author:** ![erraisahmad](https://avatars.discourse-cdn.com/v4/letter/e/bbe5ce/32.png) [@erraisahmad](https://discuss.zetetic.net/u/erraisahmad)\
**Post date:** [October 10, 2025, 1:54am UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/7 "2025-10-10T01:54:31Z")

</div>

Hi @developernotes

I am running only a PRAGMA command in post key.

```auto
public void postKey(SQLiteConnection sqLiteConnection) {
		try {
			sqLiteConnection.execute("PRAGMA cipher_migrate;", null, null);
		} catch (Exception exception) {
			if(DEBUG) {
				Log.d(TAG, " postKey PRAGMA statement execution Failed ");
				exception.printStackTrace();
			}
		}
	}

```

I don’t see any exception in preKey where I am executing another PRAGMA.

```auto
public void preKey(SQLiteConnection sqLiteConnection) {
		try {
			sqLiteConnection.execute("PRAGMA cipher_default_kdf_iter = 4000;", null, null);
		} catch (Exception exception) {
			exception.printStackTrace();
			}
		}
		
	}

```

Its only occurring for postKey

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 10, 2025, 12:16pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/8 "2025-10-10T12:16:46Z")

</div>

Hi @erraisahmad,

[Here](https://github.com/sqlcipher/sqlcipher-android/blob/master/sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/MigrateDatabaseFrom1xFormatToCurrentFormatTest.java) is an example of performing the `PRAGMA cipher_migrate` command within the `postKey` event.

---

<div class="post-metadata">

**Author:** ![R\_Rajesh](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/r_rajesh/32/2483_2.png) [@R\_Rajesh](https://discuss.zetetic.net/u/R_Rajesh)\
**Post date:** [January 14, 2026, 6:30pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/9 "2026-01-14T18:30:37Z")

</div>

Hi @developernotes

net.zetetic.database.sqlcipher.SQLiteNotADatabaseException: file is not a database (code 26): , while compiling: PRAGMA journal\_mode with latest version 4.12

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [January 14, 2026, 8:28pm UTC](https://discuss.zetetic.net/t/not-able-to-open-db-created-by-android-database-sqlciphe-3-5-7-with-new-sqlcipher-android-4-9-0/7080/10 "2026-01-14T20:28:04Z")

</div>

Hi @R_Rajesh,

Please provide additional details regarding your situation, and if it’s unrelated to the topic of this thread, please consider creating a separate posting.
