User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_11

This is an old revision of the document!


Chapter 11

Under development.

You may hear the expression “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

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.

/** 
 * Create a DRINK table with an _id PK,
 * and fields for NAME, DESCRIPTION, and 
 * IMAGE_RESOURCE_ID.
 * @param db
 */
@Override
public void onCreate(SQLiteDatabase db) {
	db.execSQL("CREATE TABLE DRINK ("
			+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ "NAME TEXT, "
			+ "DESCRIPTION TEXT, "
			+ "IMAGE_RESOURCE_ID INTEGER);");
}
android_learning/headfirst_android_development_notes/chapter_11.1458167115.txt.gz · Last modified: 2016/03/16 22:25 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki