User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_11

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_learning:headfirst_android_development_notes:chapter_11 [2016/03/16 22:54] mithatandroid_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.
-</WRAP>+
  
-You may hear the term "CRUD" used in relation to databases. And if you haven't, you have now. CRUD is a 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 also helpful to think in terms of database //schema//---the plan or layout of the database table(s).+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[[https://youtu.be/jN_YdMdjVpU?t=1m7s|sees things differently]], so the indefinite article should be "an".+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 [[https://youtu.be/jN_YdMdjVpU?t=1m7s|sees things differently]], so the indefinite article should be "an".
  
 ===== p. 444: Code block ===== ===== p. 444: Code block =====
Line 33: Line 32:
 } }
 </file> </file>
-Note that the constructor in the above class definition creates compiler rage. It gets fixed on the following page.+The constructor in the above class definition will create compiler rage---which is fixed on the following page. But if you're impatient, the solution is to invoke to parent class' ctor:<code java> 
 +    StarbuzzDatabaseHelper(Context context) { 
 +        super(context, DB_NAME, null, DB_VERSION); 
 +    }</code> 
  
 ===== p. 447: Schema definition ===== ===== 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.+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. (If you've not seen SQL before, don't worry. It's the "standard" language used by databases for conducting transactions, and the basics are explained by example in the book.)
  
 <code java> <code java>
Line 87: Line 90:
                    String[] whereArgs)                    String[] whereArgs)
 </code> </code>
 +
 +<WRAP center round important 90%>
 +Calling ''delete()'' with null ''whereClause'' and ''whereArgs'' arguments will delete all the records in the database.
 +</WRAP>
  
 ===== What about Read? ===== ===== What about Read? =====
Line 108: Line 115:
         super(context, DB_NAME, null, DB_VERSION);         super(context, DB_NAME, null, DB_VERSION);
     }     }
- 
- 
  
     @Override     @Override
Line 133: Line 138:
     @Override     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {     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);
 +    }
 +}
 +</file>
 +
 +===== 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 = "starbuzz"; // the name of our database
 +    private static final int DB_VERSION = 2; // the version of the database
 +
 +    StarbuzzDatabaseHelper(Context context){
 +        super(context, DB_NAME, null, DB_VERSION);
 +    }
 +
 +    @Override
 +    public void onCreate(SQLiteDatabase db){
 +        updateMyDatabase(db, 0, DB_VERSION);
 +    }
 +
 +    @Override
 +    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 +        updateMyDatabase(db, oldVersion, newVersion);
 +    }
 +
 +    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, and
 +             * IMAGE_RESOURCE_ID.
 +             */
 +            db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
 +                    + "NAME TEXT, "
 +                    + "DESCRIPTION TEXT, "
 +                    + "IMAGE_RESOURCE_ID INTEGER);");
 +
 +            // Insert 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);
 +        }
 +
 +        if (oldVersion < 2) {
 +            // Add a FAVORITE column.
 +            db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;");
 +        }
     }     }
  
android_learning/headfirst_android_development_notes/chapter_11.1458168871.txt.gz · Last modified: 2016/03/16 22:54 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki