SQLite For Android Source Codes

public class SQLiteHelper extends SQLiteOpenHelper {
 private static final String DATABASE_NAME = "my_database.db";
 // TOGGLE THIS NUMBER FOR UPDATING TABLES AND DATABASE

 private static final int DATABASE_VERSION = 1;
 // NAME OF TABLE YOU WISH TO CREATE
 public static final String TABLE_NAME = "my_table";
 // SOME SAMPLE FIELDS
 public static final String UID = "_id";
 public static final String NAME = "name";
 
 SQLiteHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + UID + "
  INTEGER PRIMARY KEY AUTOINCREMENT," + NAME
  + " VARCHAR(255));");
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion,
   int newVersion) {
  Log.w("LOG_TAG", "Upgrading database from version " +
   oldVersion + " to " + newVersion + ", which will destroy all old data");
  // KILL PREVIOUS TABLE IF UPGRADED
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  // CREATE NEW INSTANCE OF TABLE
  onCreate(db);
 }
}


Implementing the database helper class.

public class SQLiteExample extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // INIT OUR SQLITE HELPER
  SQLiteHelper sqh = new SQLiteHelper(this);
  // RETRIEVE A READABLE AND WRITEABLE DATABASE
  SQLiteDatabase sqdb = sqh.getWritableDatabase();
  
  
  // METHOD #1: INSERT USING CONTENTVALUE CLASS
  ContentValues cv = new ContentValues();
  cv.put(SQLiteHelper.NAME, "jason wei");
  // CALL INSERT METHOD
  sqdb.insert(SQLiteHelper.TABLE_NAME, SQLiteHelper.NAME, cv);
  
  
  // METHOD #2: INSERT USING SQL QUERY
  String insertQuery = "INSERT INTO " +
  SQLiteHelper.TABLE_NAME + " (" + SQLiteHelper.NAME + ") VALUES ('jwei')";
  sqdb.execSQL(insertQuery);

  // METHOD #1: QUERY USING WRAPPER METHOD
  Cursor c = sqdb.query(SQLiteHelper.TABLE_NAME,
   new String[] { SQLiteHelper.UID, SQLiteHelper.NAME },
    null, null, null, null, null);
  while (c.moveToNext()) {
  // GET COLUMN INDICES + VALUES OF THOSE COLUMNS
  int id = c.getInt(c.getColumnIndex(SQLiteHelper.UID));
  String name = c.getString(c.getColumnIndex(SQLiteHelper.NAME));
  Log.i("LOG_TAG", "ROW " + id + " HAS NAME " + name);
  }
  c.close();

  // METHOD #2: QUERY USING SQL SELECT QUERY
  String query = "SELECT " + SQLiteHelper.UID + ", " +
   SQLiteHelper.NAME + " FROM " + SQLiteHelper.TABLE_NAME;
  Cursor c2 = sqdb.rawQuery(query, null);
  while (c2.moveToNext()) {
  int id = c2.getInt(c2.getColumnIndex(SQLiteHelper.UID));
  String name = c2.getString(c2.getColumnIndex(SQLiteHelper.NAME));
  Log.i("LOG_TAG", "ROW " + id + " HAS NAME " + name);
  }
  c2.close();
  
  // CLOSE DATABASE CONNECTIONS
  sqdb.close();
  sqh.close();
 }
}

Comments

Popular posts from this blog

Fatal: LoadModule: error loading module 'mod_sql_mysql.c'

How to hide Android Soft Keyboard