File not a database exception when exporting from SQLite DB to SQLCipher

public static void migrateToEncrption(Context context, int i) throws IOException{
    File sqliteDbFile = context.getDatabasePath(SQLDataStore.DATABASE_NAME);
    File newFile = context.getDatabasePath(DATABASE_NAME_  + String.valueOf(i));
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(sqliteDbFile.getAbsolutePath(), "", null,         new SQLiteDatabaseHook() {
        public void preKey(SQLiteDatabase database) {

        public void postKey(SQLiteDatabase db) {
            db.execSQL("PRAGMA cipher_default_page_size = 4096");
    db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'", newFile.getAbsolutePath(), password));
//    db.execSQL("PRAGMA encrypted.cipher_page_size = 4096");
    db.execSQL("PRAGMA encrypted.auto_vacuum = 1");
    db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
    db.rawExecSQL("DETACH DATABASE encrypted;");
    int version = db.getVersion();
    db = SQLiteDatabase.openOrCreateDatabase(newFile.getAbsolutePath(), password, null);
    //noinspection ResultOfMethodCallIgnored

public class DataStore extends SQLiteOpenHelper {
    public DataStore(Context context, int i) {
        super(context.getApplicationContext(), DATABASE_NAME_  + String.valueOf(i) ,
                    null  /* cursor factory */, DATABASE_VERSION);

I’m using the above code to convert a SQLite DB to SQLCipher. The issue here is that whenever I try to set cipher_page_size for the encrypted DB, I’m getting the following error.

net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
at net.sqlcipher.database.SQLiteCompiledSql.compile(
at net.sqlcipher.database.SQLiteCompiledSql.(
at net.sqlcipher.database.SQLiteProgram.(
at net.sqlcipher.database.SQLiteQuery.(
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(
at net.sqlcipher.database.SQLiteDatabase.rawQuery(
at net.sqlcipher.database.SQLiteDatabase.keyDatabase(
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(
at net.sqlcipher.database.SQLiteDatabase.openDatabase(
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(
at <package_name>.Datastore.<init>(Line_no_of_getWritableDatabase)

Note that this doesn’t happen if I don’t set the cipher_page_size either by the commented method or the one in postKey method.

I’m using android-database-sqlcipher:3.5.9

Hi @chandraprakash

Setting cipher_default_page_size to 4096 in the postKey event of your database hook is setting the page size of the encrypted export to 4096, which will require you to set that value every time you open the connection. Unless you want a page size of 4k, you can remove that from both the hook and the subsequent PRAGMA encrypted.cipher_page_size as well.