MySQLiteHelper.java (Cont.)

import android.content.Context;
It is the interface to global information about an application environment. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

import android.database.sqlite.SQLiteDatabase;
It exposes methods to manage an SQLite database. SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.

import android.database.sqlite.SQLiteOpenHelper;
It manages database creation and version management. Developers create a subclass implementing and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary.

import android.util.Log;
It is an API for sending log output. Generally, use the following methods:
  • Log.e( ), which sends an ERROR log message and log the exception
  • Log.w( ), which sends a WARN log message and log the exception
  • Log.d( ), which sends a DEBUG log message and log the exception
  • Log.i( ), which sends an INFO log message and log the exception
  • Log.v( ), which sends a VERBOSE log message and log the exception

public class MySQLiteHelper extends SQLiteOpenHelper {
A subclass of SQLiteOpenHelper, such as MySQLiteHelper in this application, is often used to create and upgrade a database.
src/main/java/com/example/wenchen/sqlitedemo/MySQLiteHelper.java
package com.example.wenchen.sqlitedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_COMMENTS = "comments";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_COMMENT = "comment";

  private static final String DATABASE_NAME = "applicationdata";
  private static final int DATABASE_VERSION = 1;

  // create table comments (
  //   _id integer primary key autoincrement,
  //   comment text not null );   
  private static final String TABLE_CREATE = "create table "
    + TABLE_COMMENTS + "( " + COLUMN_ID + " integer primary key autoincrement, "
    + COLUMN_COMMENT + " text not null );";

  public MySQLiteHelper( Context context ) {
    super( context, DATABASE_NAME, null, DATABASE_VERSION );
  }

  @Override
  public void onCreate( SQLiteDatabase database ) {
    database.execSQL( TABLE_CREATE );
  }

  @Override
  public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
    Log.w( MySQLiteHelper.class.getName( ), "Upgrading database from version "
      + oldVersion + " to " + newVersion + ", which will destroy all old data" );
    db.execSQL( "DROP TABLE IF EXISTS" + TABLE_COMMENTS );
    onCreate( db );
  }
}




      It’s been raining cats and dogs all day.    
      I’m afraid the roof is going to leak.