How do I query SQLite Database in Android?
- by kunjaan
I successfully created the Database and inserted a row however I cannot Query it for some reason. My Droid crashes everytime.
String DATABASE_NAME = "myDatabase.db";
String DATABASE_TABLE = "mainTable";
String DATABASE_CREATE = "create table if not exists " + DATABASE_TABLE
+ " ( value VARCHAR not null);";
SQLiteDatabase myDatabase = null;
myDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
myDatabase.execSQL(DATABASE_CREATE);
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("value", "kunjan");
// Insert the row into your table
myDatabase.insert(DATABASE_TABLE, null, newValues);
String[] result_columns = new String[] { "value" };
Cursor allRows = myDatabase.query(true, DATABASE_TABLE, result_columns, null, null,
null, null, null, null);
if (allRows.moveToFirst()) {
String value = allRows.getString(1);
TextView foo = (TextView) findViewById(R.id.TextView01);
foo.setText(value);
}
allRows.close();
myDatabase.close();