Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling sqlite Write-Ahead logging in Android Pie

In Android Pie sqlite Write-Ahead logging (WAL) has been enabled by default. This is causing errors for my existing code only in Pie devices. I have been unable to turn off WAL successfully using SQLiteDatabase.disableWriteAheadLogging() or PRAGMA journal_mode due to the way I access the database. I would like to disable WAL completely with an Android setting called db_compatibility_wal_supported :

Compatibility WAL (Write-Ahead Logging) for Apps

Does anyone know how to configure this? I don't know if this file can be altered programmatically at startup or if it is changed manually.


Further Details about the problem

I have a sqlite database (20mb+ / 250k records) in my app. This db is generated using plain java on my server. It contains a food database and the user of the app can add to the database (and the server is updated). This is stored in the assets folder in android. During first installation the database is copied from assets to the app folder so that it can be written to, using effectively this method :

Copy SQLite database from assets folder

Unfortunately, once I start writing to the database using SqlDroid wal is enabled and the tables which were in the original db have vanished and only any newly created tables remain. The size of the database however is still 20mb+. All the database errors are due to the missing tables. The table copying and writing method works perfectly in versions of Android prior to Pie.

like image 735
Rockvole Avatar asked Dec 06 '18 20:12

Rockvole


1 Answers

The best and simplest way to disable WAL mode in your Database is as follows:

public class MyDbHelper extends SQLiteOpenHelper {

    //...

    @Override
    public void onOpen(SQLiteDatabase db) {
        db.disableWriteAheadLogging();  // Here the solution
        super.onOpen(db);
    }

    //...
}

This way, all access to your database will be with WAL mode disabled. As much as you open and close multiple connections throughout the implementation of your App

like image 85
Carlos Henrique Avatar answered Oct 05 '22 14:10

Carlos Henrique