开发者

Android and SQLite using the SQLiteOpenHelper

I have a SQLite database, and several tables within that datbase. I am developing a DBAdapter for each table within the database. (reference Reto Meier's Professional Android 2 Application Development, Listing 7.1).

I am using the adb shell to interface with the database from the command line and see that the database is being populated as I expect. Occasionally, I want to 开发者_高级运维drop a table so that I can ensure it's being built properly, from scratch.

The problem is that SQLiteOpenHelper only checks to see if the database exists. Is there a typical solution to writing a helper to also see that the table(s) exists? Basically once I drop a table, the helper checks to see that the database exists and assumes all is well.

Also, the CREATE_DATABASE string used in the reference above only creates the one table. Should I consider using the DBAdapter for an adapter to ALL of my tables? That doesn't seem as clean to me.

Reference Material


Is there a typical solution to writing a helper to also see that the table(s) exists?

SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='...' ORDER BY name;

(substituting in your table's name for ...)

If that returns 1, your table exists. If that returns 0, your table does not exist.

However, the helper won't help you with that. For testing purposes, I recommend getting rid of the entire database, since in production, you won't be creating individual tables on the fly, either.


Occasionally, I want to drop a table so that I can ensure it's being built properly, from scratch.

I have a instrumentation test which drops and recreates all tables. A little more work but once done a lot more helpful.

Is there a typical solution to writing a helper to also see that the table(s) exists?

In addition to what CommonsWare suggested there is also a option to use shell command to get this informations.

adb -e shell sqlite3 -batch ${DB_FILE} .table
adb -e shell sqlite3 -batch ${DB_FILE} .schema
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜