How to check SQLite file consistency (health check)

淺唱寂寞╮ 提交于 2019-12-06 13:31:55

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.

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.

I would prefer second approach on the first one both with respect to performance and ease of coding.

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