问题
This issue only applies to an SDK 28 device (emulator). My SQLite database created with SQLiteOpenHelper works perfectly in the app, however when I export it to a disk (either by means of Android studio or by the code), much of its data is lost. Changes made to this database in the app are immediately reflected in the app but are not reflected in the exported database file. The database file that I am trying to export is located here: DDMS -> file explorer -> data -> data -> my package name -> databases -> my database file. On an SDK < 28 device, the database file is exported perfectly well.
回答1:
I resolved the issue independently.
I found that the database folder in addition to the database itself contains two supplementary files. The contents of the folder looked like this:
- MyDatabase.db
- MyDatabase.db-shm
- MyDatabase.db-wal
Without these files, the database is incomplete.
Prior to the SDK 28, there is only one supplementary file and it does not impact the integrity of the database:
- MyDatabase.db-journal
The solution was to execute .setWriteAheadLoggingEnabled(false) on the singleton instance of my database in SQLiteOpenHelper:
synchronized static MyDatabase getInstance(Context context) {
if (instance == null) {
instance = new MyDatabase(context.getApplicationContext());
instance.setWriteAheadLoggingEnabled(false);
}
return (instance);
}
This restores the database logging setting used in the SDKs < 28 by default. Doing this provides me with a single database file that contains the full and up to date data not depending on supplementary files.
来源:https://stackoverflow.com/questions/53834347/exported-sqlite-database-is-not-up-to-date