Building Your Initial Database

With your schema defined, you are ready to create the database itself. Android provides some low-level methods on Context to open a database file into an instance of SQLiteDatabase: openOrCreateDatabase(…) and databaseList().

However, in practice you will always need to follow a few basic steps:

  • Check to see whether the database already exists.

  • If it does not, create it and create the tables and initial data it needs.

  • If it does, open it up and see what version of your CrimeDbSchema it has. (You may want to add or remove things in future versions of CriminalIntent.)

  • If it is an old version, upgrade it to a newer version.

Android provides the SQLiteOpenHelper class to handle all ...

Get Android Programming: The Big Nerd Ranch Guide, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.