android - 备份时的Android 9数据库日志模式WAL问题
问题描述
我的应用程序中有一些备份/恢复功能,在使用 >= Android 9 时会导致问题,SQLite 数据库确实使用 journal_mode WAL 而不是他们在旧手机上使用的 DELETE。我的 SQLLite 版本是 3.25.2
更多信息:
我正在使用完全集成到我的项目中的 Sugar ORM。Sugar ORM 使用自己的类,例如,扩展 SQLiteOpenHelper 的 SugarDb,并且该类是只读的。
问题:
- 如何以及在哪里检查 *.db 文件的 journal_mode 是否为 WAL?
- SQLite journal_mode 为 WAL 时如何正确复制/恢复 *.db 文件?
- 对于所有手持设备,如何以及在何处将 SQLite journal_mode 从 WAL 切换到 DELETE?
解决方案
Some good background reading https://sqlite.org/wal.html
Answer to 3.
Use https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#disableWriteAheadLogging() in the onConfigure
of SQLiteOpenHelper (or in your case you probably have to extend and override the SugarDb class to do that.
You will probably have to extend getInstance
as well to return your an instance of your own class (and not call super I think in that override), then where you call SugarDb.getInstance()
you call getInstance
on your extended class.
Answer to 2. As @CommonsWare says if your DB is closed correctly then all data is commited to the DB file from the Wal file on close.
Update: Looking at https://github.com/chennaione/sugar/blob/master/library/src/main/java/com/orm/SugarDb.java
For every time you have called SugarDb.getReadableDatabase()
or SugarDb.getWritableDatabase()
you should call SugarDb.close()
The easiest way to do that is as soon as you have finished the current database operations as you cannot query it's connection count, or you could maintain your own count and close at various times in the apps lifecycle like onPause
or onDestroy
or before your try to backup
Answer to 1. https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#isWriteAheadLoggingEnabled()
推荐阅读
- php - 在 PHP 中使用循环求和
- flutter - 如何处理flutter中的动态下拉列表?
- java - 如何在android中解决“尝试重新打开关闭的SQLite数据库”
- java - 在 Liferay 中使用 OkHttp 来使用外部的 REST API。OSGi 的问题
- c# - Newtonsoft.Json.JsonSerializationException('从'Goodbuy.Models.product'上的'Id'获取值时出错。')将领域对象序列化为json
- excel - Excel 2019 - 如何使用 VBA 检测在 Excel 主菜单“文件”下拉菜单上所做的选择?
- java - 我断言在 Java 中不会发生错误。我该如何编码?
- android - 如何在 Retrofit Android 中使用 JSON 响应管理模型类?
- haskell - 将函数应用于 Haskell 中的元素列表
- c++ - 将泛型函数分配给函数指针 struct memeber