This is an old revision of the document!
Table of Contents
Chapter 11
Under development.
You may hear the term “CRUD” used in relation to databases. And if you haven't, you have now. CRUD is a acronym for Create (or Insert), Read, Update, and Delete—the four main operations that are done on database records. It's also helpful to think in terms of database schema—the plan or layout of the database table(s).
The book consistently uses “a” as the indefinite article ahead of “SQLite”—most probably because they assume a pronunciation of “sequel lite.” The primary author of “SQLite” sees things differently, so the indefinite article should be “an”.
p. 444: Code block
- StarbuzzDatabaseHelper.java
package com.hfad.starbuzz; import android.database.sqlite.SQLiteOpenHelper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; class StarbuzzDatabaseHelper extends SQLiteOpenHelper { StarbuzzDatabaseHelper(Context context) { // This constructor isn't liked by the compiler. // It'll be fixed shortly. } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
Note that the constructor in the above class definition creates compiler rage. It gets fixed on the following page.
p. 447: Schema definition
You create the schema for your database in the SQLiteOpenHelper
's onCreate
method using plain old SQL on the SQLite database that's passed in.
@Override public void onCreate(SQLiteDatabase db) { /** * Create a DRINK table with an _id PK, * and fields for NAME, DESCRIPTION, and * IMAGE_RESOURCE_ID. */ db.execSQL("CREATE TABLE DRINK (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "NAME TEXT, " + "DESCRIPTION TEXT, " + "IMAGE_RESOURCE_ID INTEGER);"); }
p. 448: Create
Use a ContentValues
object to accumulate data for the new record and then use SQLiteDatabase
's insert
method to do the new record creation/insertion. Here it's being done in a private method:
/** * Create a new drink record in the DRINK table. */ private static void insertDrink(SQLiteDatabase db, String name, String description, int resourceId) { ContentValues drinkValues = new ContentValues(); drinkValues.put("NAME", name); drinkValues.put("DESCRIPTION", description); drinkValues.put("IMAGE_RESOURCE_ID", resourceId); db.insert("DRINK", null, drinkValues); }
p. 449: Update
Update existing records in a table with SQLiteDatabase
's update
method:
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
p. 450: Delete
Delete records in a table with SQLiteDatabase
's delete
method:
public int delete(String table, String whereClause, String[] whereArgs)
Calling delete with null whereClause
e and whereArgs
arguments will delete all the records in the database.
What about Read?
It's covered in Chapter 12.
p. 452: Code block
- StarbuzzDatabaseHelper.java
package com.hfad.starbuzz; import android.content.ContentValues; import android.database.sqlite.SQLiteOpenHelper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; class StarbuzzDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "starbuzz"; // the name of the DB private static final int DB_VERSION = 1; // the version of the DB StarbuzzDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { /** * Create a DRINK table with an _id PK, * and fields for NAME, DESCRIPTION, and * IMAGE_RESOURCE_ID. */ db.execSQL("CREATE TABLE DRINK (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "NAME TEXT, " + "DESCRIPTION TEXT, " + "IMAGE_RESOURCE_ID INTEGER);"); // Insert some drinks into the DRINKS table: insertDrink(db, "Latte", "Espresso and steamed milk", R.drawable.latte); insertDrink(db, "Cappuccino", "Espresso, hot milk and steamed-milk foam", R.drawable.cappuccino); insertDrink(db, "Filter", "Our best drip coffee", R.drawable.filter); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } private static void insertDrink(SQLiteDatabase db, String name, String description, int resourceId) { ContentValues drinkValues = new ContentValues(); drinkValues.put("NAME", name); drinkValues.put("DESCRIPTION", description); drinkValues.put("IMAGE_RESOURCE_ID", resourceId); db.insert("DRINK", null, drinkValues); } }