extends SQLiteOpenHelper - Android android.database.sqlite

Android examples for android.database.sqlite:SQLiteOpenHelper

Description

extends SQLiteOpenHelper

Demo Code

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

class MyUnreadHelper extends SQLiteOpenHelper {
  private static final String DB_NAME = "sms.db";
  private static final String TBL_NAME = "SmsTbl";
  private static final int SMS_ID_NUM = 1;
  private static final int SMS_THREAD_ID_NUM = 2;
  private static final String CREATE_TBL = " CREATE TABLE IF NOT EXISTS "
      + " SmsTbl(_id integer primary key autoincrement,mSmsId text,mSmsThreadId text, mAddress text, mSmsBody text"
      + ", mSmsDateStr text,  mSmsType text, mSmsPerson text, mSmsRead text) ";

  private SQLiteDatabase mDb;
  private static MyUnreadHelper sMyUnreadHelper;

  private MyUnreadHelper(Context c) {
    super(c, DB_NAME, null, 2);
  }/* ww  w  .  j ava2  s  . c o  m*/

  public static MyUnreadHelper getInstance(Context c) {
    if (sMyUnreadHelper == null) {
      sMyUnreadHelper = new MyUnreadHelper(c);
    }
    return sMyUnreadHelper;
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    this.mDb = db;
    db.execSQL(CREATE_TBL);
  }

  public void insert(ContentValues values) {
    if (mDb == null) {
      mDb = getWritableDatabase();
    }
    try {
      mDb.insert(TBL_NAME, null, values);
    } catch (Exception e) {
      // TODO: handle exception
    }
    // db.close();
  }

  public boolean hasflag(String smsId, String smsthreadId) {
    if (mDb == null) {
      mDb = getWritableDatabase();
    }
    Cursor cr = mDb.query(TBL_NAME, null, null, null, null, null, null);
    boolean flag = false;
    if (cr != null) {
      try {
        while (cr.moveToNext()) {
          if (smsId != null) {
            if (smsId.equalsIgnoreCase(cr.getString(SMS_ID_NUM))
                && smsthreadId.equalsIgnoreCase(cr.getString(SMS_THREAD_ID_NUM))) {
              flag = true;
              return flag;
            }
          }
        }
      } catch (Exception e) {
      } finally {
        if (cr != null) {
          cr.close();
          cr = null;
        }
      }
    }
    return flag;
  }

  public void del(int id) {
    if (mDb == null) {
      mDb = getWritableDatabase();
    }
    try {
      mDb.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });
    } catch (Exception e) {
      // TODO: handle exception
    }
  }

  public void close() {
    if (mDb != null) {
      mDb.close();
    }
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  }
}

Related Tutorials