Disabling sqlite Write-Ahead logging in Android Pie

前端 未结 5 648
天涯浪人
天涯浪人 2021-01-04 18:56

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

相关标签:
5条回答
  • 2021-01-04 19:02

    If you are using Room, you won't have direct access to the database but you can instead set the journal mode when you are building/opening the database:

    db = Room.databaseBuilder(context, Database.class, "Database")
             .setJournalMode(JournalMode.TRUNCATE)
             .build();
    
    0 讨论(0)
  • 2021-01-04 19:07

    @Rockvole please share error that you are facing, that help us to find appropriate solution.

    Mean while, i understand that you want to close that WAL in android pie and you are using "SQLDroid" lib to create Sqlite DB.

    This lib internally using "SQLiteDatabase" to store data locally, I think you need to call "SQLiteDatabase.disableWriteAheadLogging()" in "SQLiteDatabase" class where DB instance created the package name is "package org.sqldroid;"

    or Get internal SQLiteDatabase instance and call disableWriteAheadLogging().

    Second solution is create "config.xml" inside values folder and wirte "<bool name="db_compatibility_wal_supported">false</bool>" and run and check its work.

    0 讨论(0)
  • 2021-01-04 19:18

    I finally found the answer. It seems that the database errors I was receiving is not directly related to WAL. It is because the widely used code to copy a database from assets has a bug in it where the database is left open during the copy operation. This only started causing a problem in Android P. The solution is to close the database after you get the database file name.

    SQLiteDatabase destinationDatabase = sqLiteOpenHelper.getWritableDatabase();
    String dbFileName=destinationDatabase.getPath();
    destinationDatabase.close();
    // Now write the destinationDatabase using the dbFileName
    

    This is detailed more here : Android P - 'SQLite: No Such Table Error' after copying database from assets

    0 讨论(0)
  • 2021-01-04 19:20

    one cannot use SQLDroidDriver.ADDITONAL_DATABASE_FLAGS, simply because there is no constant available, which would negate flag ENABLE_WRITE_AHEAD_LOGGING.

    WAL can still be disabled by creating either of these scenarios:

    a) set flag OPEN_READONLY (applies to situations where R/O access does suffice).

    b) run PRAGMA journal_mode=DELETE as the first query, in order to override PRAGMA journal_mode=WAL.

    c) file an issue against SQLDroidConnection.java, in order to have .enableWriteAheadLogging() and .disableWriteAheadLogging() supported on the driver-level.

    0 讨论(0)
  • 2021-01-04 19:26

    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

    0 讨论(0)
提交回复
热议问题