Monday, 4 February 2013

Sample SqlLite Query Android

 MyDbHelper.java
public class MyDbHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "mydb";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "error";
    // public static final int ORIGIN = 1;
    public static final String ORIGIN = "origin";
    public static final String ERROR_STATUS = "eStatus";
    public static final String ERROR_MESSAGE = "eMessage";
    public static final String ERROR_TIMESTAMP = "etimestamp";
    public static final String ERROR_CAUSECODE = "ecausecode";
    public static final String ERROR_DISPLAYCODE = "edisplaycode";
    public static final String ERROR_NOTEID = "enoteid";
    private static final String STRING_CREATE = "CREATE TABLE " + TABLE_NAME
            + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + ERROR_STATUS
            + " TEXT, " + ERROR_MESSAGE + " TEXT ," + ERROR_TIMESTAMP
            + " TEXT," + ERROR_CAUSECODE + " TEXT, " + ERROR_DISPLAYCODE
            + " TEXT, " + ORIGIN + " TEXT, " + ERROR_NOTEID + " TEXT " + ");";

    public MyDbHelper(Context context) {

        super(context, DB_NAME, null, DB_VERSION);
        System.out.println("db initialised");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(STRING_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}




mHelper = new MyDbHelper(context);
 mDb = mHelper.getWritableDatabase();

Cursor cur = mDb.rawQuery("SELECT * from " + MyDbHelper.TABLE_NAME,
                    null);
mDb.update(MyDbHelper.TABLE_NAME, cv, MyDbHelper.ERROR_NOTEID
                        + "=?", new String[] { testnoteId });


Cursor curcheck = mDb.rawQuery("SELECT "
                        + MyDbHelper.ERROR_STATUS + ","
                        + MyDbHelper.ERROR_CAUSECODE + ","
                        + MyDbHelper.ERROR_DISPLAYCODE + ","
                        + MyDbHelper.ERROR_MESSAGE + "," + MyDbHelper.ORIGIN
                        + " FROM " + MyDbHelper.TABLE_NAME + " WHERE "
                        + MyDbHelper.ERROR_NOTEID + "=?",
                        new String[] { idshared });


No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...