SQL Helper Class Example

06.06.2011

package com.assassinmobile.android;

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

public class SQLHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = “localdb2.db”;
private static final int DATABASE_VERSION = 1;

// Table name
public static final String USERS_TABLE = “users”;

//User columns
public static final String USER_ID = “user_id”;
public static final String USER_CODENAME = “user_codename”;
public static final String USER_EMAIL = “user_email”;
public static final String USER_PASSWORD = “user_password”;

public SQLHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql_user = “create table ” + USERS_TABLE +
“( ” + USER_ID + ” integer primary key autoincrement, ” +
USER_CODENAME + ” text not null, ” +
USER_EMAIL + ” text not null, ” +
USER_PASSWORD + ” text not null);”;

db.execSQL(sql_user);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}