How do I query SQLite Database in Android?
Posted
by kunjaan
on Stack Overflow
See other posts from Stack Overflow
or by kunjaan
Published on 2010-06-02T02:57:11Z
Indexed on
2010/06/02
3:03 UTC
Read the original article
Hit count: 270
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();
© Stack Overflow or respective owner