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:

  1. Check to see if the database already exists.

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

  3. 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.)

  4. If it is an old version, run code to upgrade it to a newer version.

Android provides the SQLiteOpenHelper class to handle ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.