Getting Persistent with Data Storage

267

Chapter 12: Getting Persistent with Data Storage

Creating a Java file to hold the database code The first thing to do is create a Java file in your Android project that will house all the database-centric code. I am going to name my file RemindersDbAdapter.java. The name RemindersDbAdapter was chosen because this is a simple implementation of the adapter software engineering pattern. The adapter pattern is simply a wrapper class that allows incompatible classes to communicate with each other. Think of the adapter pattern as the wires and ports behind your television and DVD player. The cables plug into ports that are essentially adapters that allow devices to communicate that normally couldn’t. They share a common interface. By creating an adapter to handle the database communication, you can communicate with this class via the programming language of Java while this adapter class does the trans- lation and adapts certain Java requests into SQLite-specific commands. Defining the key elements Before you open and create your database, I need to define a few key fields. Type the code from Listing 12-1 into your RemindersDbAdapter class. Listing 12-1: The Constants, Fields, and Constructors of the RemindersDbAdapter Class private static final String DATABASE_NAME = “data”; ➝ 1 private static final String DATABASE_TABLE = “reminders”; ➝ 2 private static final int DATABASE_VERSION = 1; ➝ 3 public static final String KEY_TITLE = “title”; ➝ 5 public static final String KEY_BODY = “body”; public static final String KEY_DATE_TIME = “reminder_date_time”; public static final String KEY_ROWID = “_id”; ➝ 8 private DatabaseHelper mDbHelper; ➝ 11 private SQLiteDatabase mDb; ➝ 12 continued 268 Part III: Creating a Feature-Rich Application Listing 12-1 continued private static final String DATABASE_CREATE = ➝ 14 “create table “ + DATABASE_TABLE + “ “ + KEY_ROWID + “ integer primary key autoincrement, “ + KEY_TITLE + “ text not null, “ + KEY_BODY + “ text not null, “ + KEY_DATE_TIME + “ text not null;”; private final Context mCtx; ➝ 21 public RemindersDbAdapterContext ctx { ➝ 23 this.mCtx = ctx; } Each line is explained in detail here: ➝ 1 This is the physical name of the database that will exist in the Android file system. ➝ 2 This is the name of the database table that will hold the tasks. I cover the table and how to set it up in the SQL Table section that follows. ➝ 3 This is the version of the database. If you were to update the schema of your database, you would increment this and pro- vide an implementation of the onUpgrade method of the DatabaseHelper. I create the database helper in the “Creating the database table,” section later in this chapter. ➝ 5 – ➝ 8 These lines define the column names of the table that is described in the “Visualizing the SQL table” section that follows. ➝ 11 This is the classwide DatabaseHelper instance variable. The DatabaseHelper is an implementation of the SQLiteOpen- Helper class in Android. The SQLiteOpenHelper class helps with the creation and version management of the SQLite database. ➝ 12 This is the class-level instance of the SQLite database object that allows you to create, read, update, and delete records. ➝ 14 This line defines the create script for the database. I’m concat- enating the various values from the previous lines to create the various columns. Each component of the script is explained in the “Visualizing the SQL table,” section that follows. ➝ 21 This is the Context object that will be associated with the SQLite database object. ➝ 23 The Context object is set via the constructor of the class. The SQL database is now ready to be created with the DATABASE_CREATE script as defined previously. 269

Chapter 12: Getting Persistent with Data Storage