Exported SQLite database is not up to date

徘徊边缘 提交于 2019-12-13 03:47:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!