在DBOpenHelper中
关闭wal模式:
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
// 禁用wal
db.disableWriteAheadLogging();
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
// 禁用wal
db.disableWriteAheadLogging();
}
}
开启 WAL + 异步 Checkpoint
// 以 WAL 模式打开 DB
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabaseInWalMode(path, password, cipher, null);
// 开启异步 Checkpoint
db.setAsyncCheckpointEnabled(true);
或者
// 在 SQLiteOpenHelper 中打开 WAL
SQLiteOpenHelper helper = new SQLiteOpenHelper() {
// 重载 onCreate 等方法
};
helper.setWriteAheadLoggingEnabled(true);
// 获取 DB 对象,然后开启异步 Checkpoint
SQLiteDatabase db = helper.getWritableDatabase();
db.setAsyncCheckpointEnabled(true);