Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 14  SQLite Databases


Debugging database issues


When writing code dealing with a SQLite database, you will sometimes need to tweak the layout of
the database. For example, in an upcoming chapter you will add a suspect for each crime. This will
require an additional column on the crime table. The “right” way to do this is to write code in your
SQLiteOpenHelper to bump the version number, and then update the tables inside onUpgrade(...).


Well, the “right” way involves a fair amount of code – code that is ridiculous to write when you are
only trying to get version 1 or 2 of the database right. In practice, the best thing to do is destroy the
database and start over, so that SQLiteOpenHelper.onCreate(...) is called again.


The easiest way to destroy your database is to delete the app off your device. And the easiest way to
delete the app on stock Android is to go to the application browser and drag CriminalIntent’s icon up to
where it says Uninstall at the top of screen. (The process may be different if your version of Android is
different from stock Android.) Then you will see a screen similar to the one shown in Figure 14.6.


Figure 14.6  Deleting an app


Remember this trick if you run into any issues with your database tables in this chapter.

Free download pdf