MySQLiteHelper.java


The script MySQLiteHelper.java is responsible for creating the database. The onUpdate( ) method simply deletes all existing data and re-creates the table. It also defines several constants for the table name and the table columns.

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 );
  }
}




      I have a stepladder because my real ladder left when I was just a kid.