Android SqlChiper Upgrade from 4.5.4 to 4.12.0 for 16kb devices support

Hello,
I am upgrading sqlChiper library in my project to support 16kb devicces,
from: implementation 'net.zetetic:android-database-sqlcipher:4.5.4@aar'
to: implementation 'net.zetetic:sqlcipher-android:4.12.0@aar'.

Migaration is failing after app update.

SQLiteDatabase.openOrCreateDatabase(
    databaseFile,
    passkey,
    null,
    null,object : SQLiteDatabaseHook{
        override fun preKey(connection: SQLiteConnection) {
        }

        override fun postKey(connection: SQLiteConnection) {
            connection.executeRaw("PRAGMA cipher_migrate;", null, null)
            connection.executeRaw("PRAGMA journal_mode=wal;",null,null)
        }

    },
)

PRAGMA cipher_migrate; is returning 0 after app upgrade.

net.zetetic.database.sqlcipher.SQLiteNotADatabaseException: file is not a database (code 26): , while compiling: PRAGMA journal_mode.

please help on this, I am stuck since few days.

Hi @R_Rajesh,

You should use executeForLong instead and check the result code. An example can be found here [1].


  1. sqlcipher-android/sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/MigrateDatabaseFrom1xFormatToCurrentFormatTest.java at master · sqlcipher/sqlcipher-android · GitHub ↩︎

its returning 0 value for executeForLong

Hi @developernotes

I’m getting net.zetetic.database.sqlcipher.SQLiteNotADatabaseException: file is not a database (code 26): , while compiling: PRAGMA journal_mode

SQLiteDatabase.openOrCreateDatabase(
databaseFile,
passkey,
null,
null,object : SQLiteDatabaseHook{
override fun preKey(connection: SQLiteConnection) {
}

    override fun postKey(connection: SQLiteConnection) {
        connection.executeRaw("PRAGMA cipher_migrate;", null, null)
        connection.executeRaw("PRAGMA journal_mode=wal;",null,null)
    }

},

)

Please help me on this

Hi @R_Rajesh,

What happens if you don’t set the journal mode? Are you able to access the database file contents? Additionally, your code example still shows your usage of executeRaw for PRAGMA cipher_migrate, you need to use executeForLong.

HI @developernotes

Observed behavior

  • I removed the following line:
    PRAGMA journal_mode = WAL;

  • When I execute only the following pragma using executeForLong
    PRAGMA cipher_migrate;
    it returns 1, which indicates the migration succeeds.
    After this change, when I attempt to query the database, I encounter a runtime exception.

Query code

SQLiteDatabase.openDatabase(
    databaseFile.path,
    keyValue,
    null,
    SQLiteDatabase.OPEN_READWRITE and SQLiteDatabase.CREATE_IF_NECESSARY,
    null, object : SQLiteDatabaseHook {
        override fun preKey(connection: SQLiteConnection?) {
            // Pre-key hook for any setup before database decryption
        }

        override fun postKey(connection: SQLiteConnection?) {
            result = connection?.executeForLong("PRAGMA cipher_migrate;", null, null)
        }
    }
)

After executing this line in SqlConnection.java

if(mConfiguration.password != null && mConfiguration.password.length > 0) {
            executeForLong("SELECT COUNT(*) FROM sqlite_schema;", null, null);
        }

the following exception is thrown:

android.database.sqlite.SQLiteOutOfMemoryException: out of memory (code 7),

while compiling: SELECT COUNT(*) FROM sqlite_schema;

Questions

  • What could cause an out of memory error while compiling a query against sqlite_schema?

Any insight into what’s happening internally or how to fix this would be greatly appreciated.

Thanks in advance.

Hi @R_Rajesh,

I have a few follow-up questions:

  • What version of SQLCipher was used to create the SQLCipher database you are attempting to open?
  • Were any non-standard runtime configurations used when creating/accessing the database file previously?
  • Are you able to open this SQLCipher database using the SQLCipher version of DB Browser for SQLite?

Hello,

Please find our responses below:

  • SQLCipher version used to create the database:
    The database was originally created using SQLCipher 3.5.4.

  • Migration history:
    We later migrated the database from SQLCipher 3.5.4 to 4.5.4. The migration completed successfully, and we did not encounter any issues during or after the process.

  • Current SQLCipher version in use:
    We are currently using SQLCipher 4.12.0.

  • Runtime configuration:
    No non-standard or custom runtime configurations were used when creating, migrating, or accessing the database. Default SQLCipher settings were applied.

  • DB Browser for SQLite (SQLCipher):
    We have not observed issues opening the database with SQLCipher-compatible tools.

Let us know if you need any additional details.

Hello @R_Rajesh,

I have a few more questions that may help us better understand the situation:

  • What is the full logcat output when attempting to open a connection?
  • How much disk space is available on the device?
  • How large is the database file?
  • Where is the database password stored?