android_learning:headfirst_android_development_notes:chapter_11
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
android_learning:headfirst_android_development_notes:chapter_11 [2016/03/16 22:24] – [p. 444: Code block] mithat | android_learning:headfirst_android_development_notes:chapter_11 [2016/04/23 22:23] (current) – [p. 444: Code block] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Chapter 11 ====== | ====== Chapter 11 ====== | ||
- | <WRAP center round info 60%> | + | ===== Foreword ===== |
- | Under development. | + | If you are not familiar with the term CRUD as applied to databases, it is an acronym for **C**reate (or Insert), **R**ead, **U**pdate, and **D**elete---the four main operations that are done on database records. It's often helpful to approach learning database stuff with these four primary operations in mind. |
- | </ | + | |
- | You may hear the expression " | + | It's also helpful to think in terms of database // |
- | The book consistently uses " | + | The book consistently uses " |
===== p. 444: Code block ===== | ===== p. 444: Code block ===== | ||
Line 33: | Line 32: | ||
} | } | ||
</ | </ | ||
- | Note that the constructor in the above class definition | + | The constructor in the above class definition |
+ | StarbuzzDatabaseHelper(Context context) { | ||
+ | super(context, | ||
+ | }</ | ||
- | ===== p. 447 ===== | + | |
- | You create the schema for your database in the '' | + | ===== p. 447: Schema definition |
+ | You create the schema for your database in the '' | ||
<code java> | <code java> | ||
- | /** | ||
- | * Create a DRINK table with an _id PK, | ||
- | * and fields for NAME, DESCRIPTION, | ||
- | * IMAGE_RESOURCE_ID. | ||
- | * @param db | ||
- | */ | ||
@Override | @Override | ||
public void onCreate(SQLiteDatabase db) { | public void onCreate(SQLiteDatabase db) { | ||
+ | /** | ||
+ | * Create a DRINK table with an _id PK, | ||
+ | * and fields for NAME, DESCRIPTION, | ||
+ | * IMAGE_RESOURCE_ID. | ||
+ | */ | ||
db.execSQL(" | db.execSQL(" | ||
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, | + "_id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
Line 55: | Line 57: | ||
</ | </ | ||
+ | ===== p. 448: Create ===== | ||
+ | Use a '' | ||
+ | |||
+ | <code java> | ||
+ | /** | ||
+ | * Create a new drink record in the DRINK table. | ||
+ | */ | ||
+ | private static void insertDrink(SQLiteDatabase db, String name, | ||
+ | String description, | ||
+ | ContentValues drinkValues = new ContentValues(); | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | db.insert(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== p. 449: Update ===== | ||
+ | Update existing records in a table with '' | ||
+ | <code java> | ||
+ | public int update (String table, | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== p. 450: Delete ===== | ||
+ | Delete records in a table with '' | ||
+ | <code java> | ||
+ | public int delete(String table, | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | <WRAP center round important 90%> | ||
+ | Calling '' | ||
+ | </ | ||
+ | |||
+ | ===== What about Read? ===== | ||
+ | It's covered in Chapter 12. | ||
+ | |||
+ | ===== p. 452: Code block ===== | ||
+ | <file java 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 = " | ||
+ | private static final int DB_VERSION = 1; // the version of the DB | ||
+ | |||
+ | StarbuzzDatabaseHelper(Context context) { | ||
+ | super(context, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCreate(SQLiteDatabase db) { | ||
+ | /** | ||
+ | * Create a DRINK table with an _id PK, | ||
+ | * and fields for NAME, DESCRIPTION, | ||
+ | * IMAGE_RESOURCE_ID. | ||
+ | */ | ||
+ | db.execSQL(" | ||
+ | + "_id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
+ | + "NAME TEXT, " | ||
+ | + " | ||
+ | + " | ||
+ | |||
+ | // Insert some drinks into the DRINKS table: | ||
+ | insertDrink(db, | ||
+ | insertDrink(db, | ||
+ | R.drawable.cappuccino); | ||
+ | insertDrink(db, | ||
+ | } | ||
+ | | ||
+ | @Override | ||
+ | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
+ | } | ||
+ | |||
+ | private static void insertDrink(SQLiteDatabase db, String name, | ||
+ | String description, | ||
+ | ContentValues drinkValues = new ContentValues(); | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | db.insert(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== p. 467-468: Code block ===== | ||
+ | <file java StarbuzzDatabaseHelper.java> | ||
+ | package com.hfad.starbuzz; | ||
+ | |||
+ | import android.content.ContentValues; | ||
+ | import android.content.Context; | ||
+ | import android.database.sqlite.SQLiteDatabase; | ||
+ | import android.database.sqlite.SQLiteOpenHelper; | ||
+ | |||
+ | class StarbuzzDatabaseHelper extends SQLiteOpenHelper{ | ||
+ | private static final String DB_NAME = " | ||
+ | private static final int DB_VERSION = 2; // the version of the database | ||
+ | |||
+ | StarbuzzDatabaseHelper(Context context){ | ||
+ | super(context, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCreate(SQLiteDatabase db){ | ||
+ | updateMyDatabase(db, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
+ | updateMyDatabase(db, | ||
+ | } | ||
+ | |||
+ | private void updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
+ | if (oldVersion < 1) { | ||
+ | /** | ||
+ | * Create a DRINK table with an _id PK, | ||
+ | * and fields for NAME, DESCRIPTION, | ||
+ | * IMAGE_RESOURCE_ID. | ||
+ | */ | ||
+ | db.execSQL(" | ||
+ | + "NAME TEXT, " | ||
+ | + " | ||
+ | + " | ||
+ | |||
+ | // Insert drinks into the DRINKS table: | ||
+ | insertDrink(db, | ||
+ | insertDrink(db, | ||
+ | R.drawable.cappuccino); | ||
+ | insertDrink(db, | ||
+ | } | ||
+ | |||
+ | if (oldVersion < 2) { | ||
+ | // Add a FAVORITE column. | ||
+ | db.execSQL(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void insertDrink(SQLiteDatabase db, String name, | ||
+ | String description, | ||
+ | ContentValues drinkValues = new ContentValues(); | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | drinkValues.put(" | ||
+ | db.insert(" | ||
+ | } | ||
+ | } | ||
+ | </ |
android_learning/headfirst_android_development_notes/chapter_11.1458167096.txt.gz · Last modified: 2016/03/16 22:24 by mithat