0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So, I really feel dumb..but what's the shell command to get rid of the db?
rm /data/data/your.package.here/databases/your.db, substituting in appropriate values for your.package.here and your.db. Or, use the File Manager in DDMS (standalone or Eclipse perspective) and nuke it that way.
the rm will only work in the emulator and on rooted phones. On normal phones you will need to do an adb uninstall which will remove the /data/data/your.package.here directory.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.