导出的SQLite数据库不是最新的 [英] Exported SQLite database is not up to date

查看:53
本文介绍了导出的SQLite数据库不是最新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题仅适用于SDK 28设备(仿真器).我使用SQLiteOpenHelper创建的SQLite数据库在应用程序中可以完美运行,但是当我将其导出到磁盘(通过Android Studio或通过代码)时,其许多数据都会丢失.在应用程序中对此数据库所做的更改将立即反映在应用程序中,但不会反映在导出的数据库文件中.我要导出的数据库文件位于以下位置:DDMS->文件浏览器->数据->数据->我的包名称->数据库->我的数据库文件.在SDK上<28台设备,数据库文件可以很好地导出.

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.

推荐答案

我独立解决了该问题.

我发现数据库文件夹除了数据库本身之外还包含两个补充文件.文件夹的内容如下:

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.

在SDK 28之前,只有一个补充文件,它不会影响数据库的完整性:

Prior to the SDK 28, there is only one supplementary file and it does not impact the integrity of the database:

  • MyDatabase.db-journal

解决方案是在SQLiteOpenHelper中对数据库的单例实例执行.setWriteAheadLoggingEnabled(false):

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);
}

这将恢复SDK中<中使用的数据库日志记录设置.默认为28.这样做为我提供了一个数据库文件,其中包含不依赖于补充文件的完整和最新数据.

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.

这篇关于导出的SQLite数据库不是最新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆