如何检查SQLite文件一致性(健康检查)
我在 Android 应用程序中使用 SQLite 数据库文件,该文件是使用以下方法从存储在资产文件夹中的多个部分创建的: ReignDesign - 在 Android 应用程序中使用您自己的 SQLite 数据库
组装过程完成后,我想检查数据库是否正确合并。我想到了以下方法:
- MD5 哈希比较
- 检查表存在性和条目计数
您会推荐哪种方法?有更好的办法吗?
谢谢,
菲利普
I am using a SQLite database file in my Android application that is created from several pieces stored in the assets folder by using the following approach:
ReignDesign - Using your own SQLite database in Android applications
On completion of the assemby process I would like to check if the database was correctly merged. The following approaches crossed my mind:
- MD5 hash comparison
- Checking table existence and entry counts
Which approach would you recommend? Is there a better way?
Thanks,
Philipp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://www.sqlite.org/pragma.html#pragma_integrity_check。
http://www.sqlite.org/pragma.html#pragma_integrity_check.
在 Api 11 中,SQLiteDatabase 有一个方法 isDatabaseIntegrityOk 用于检查 SQLite 数据库的完整性:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#isDatabaseIntegrityOk()
和源代码可在此处获取:
https://github.com/android/platform_frameworks_base/blob/ics-mr1-release/core/java/android/database/sqlite/SQLiteDatabase.java#L2585
在旧设备上向后移植很容易。
In Api 11, SQLiteDatabase has a method isDatabaseIntegrityOk for checking integrity of SQLite databases :
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#isDatabaseIntegrityOk()
And source code is avaible here :
https://github.com/android/platform_frameworks_base/blob/ics-mr1-release/core/java/android/database/sqlite/SQLiteDatabase.java#L2585
It's easy to backport on old devices.
我在开发的应用程序中遇到了类似的情况。有多种方法可以做到这一点;但最终我不再担心到底什么以及如何最好地衡量数据库完整性,而是专注于“我的数据库是否可供我的应用程序使用”。
所以基本上我测试是否:1)我可以正确打开 SQLite 数据库2)我可以在数据库上执行查询以及3)预定义查询的结果是否返回预期的结果。
所以基本上:包括一个包含已知 ID 记录的表,该记录给出了您知道的值,然后尝试读取该值。检查桌子数量是不可能的。
也就是说,我希望这里对数据库系统有深入了解的人能够解释 a) PRAGMAintegrity_check 的作用以及 b) 与手动表检查相比,它的可靠性如何以及效率如何。
I had a similar situation in an application I'm developing. There are a variety of ways of doing it; but in the end I stopped worrying about what exactly and how to best measure database integrity, and instead focused on 'is my database usable by my application'.
So basically I test whether: 1) I can open the SQLite database properly 2) I can perform queries on the database and 3) Whether the result for a pre-defined query returns what is expected.
So basically: include a table with a record of known ID that gives a value that you know, then try to read that value. Checking the table count can't hope.
That said I'm hoping someone here with a good knowledge of DB systems will explain a) exactly what
PRAGMA integrity_check
does and b) how reliable it is and how efficient it is compared with manual table checks.就性能和编码简易性而言,我更喜欢第二种方法而不是第一种方法。
I would prefer second approach on the first one both with respect to performance and ease of coding.