Can anyone look at my attached code and tell me why I am not inserting or selecting from the database any values. I can’t even tell which I am not doing because I am not getting any errors. All I know is that when I run the select query the cursor contains nothing.
Thanks
SQLiteDatabase db = new DatabseHelper(this).getWritableDatabase();
// db.execSQL(mDATABASE_CREATE);
String id = "1";
String feedback = "this is some feedback";
String expiredAt = "12:00";
String priority = "1";
String read = "false";
String sql = "INSERT INTO feedbackTable (_id, feedback, expiredAt, priority, read) VALUES (\""+id+"\", \""+feedback+"\", \""+expiredAt+"\", \""+priority+"\", \""+read+"\")";
db.rawQuery(sql, null);
Cursor result = db.rawQuery("SELECT * FROM feedbackTable", null);
result.moveToFirst();
String rfeedback="";
while(!result.isAfterLast()){
rfeedback=result.getString(1);
}
Log.e("returned feedback", "inserted feedback "+rfeedback);
db.close();
Second issue after changing above code:
SQLiteDatabase db = new DatabseHelper(this).getWritableDatabase();
String id = "1";
String feedback = "this is some feedback";
String expiredAt = "12:00";
String priority = "1";
String read = "false";
String sql = "INSERT INTO feedbackTable (_id, feedback, expiredAt, priority, read) VALUES (\""+id+"\", \""+feedback+"\", \""+expiredAt+"\", \""+priority+"\", \""+read+"\");";
ContentValues values = new ContentValues(5);
values.put("_id", "1");
values.put("feedback", "some feedback");
values.put("expiredAt", "12:00");
values.put("priority", "1");
values.put("read", "false");
db.insert("feedbackTable", "_id", values);
Cursor result = db.rawQuery("SELECT * FROM feedbackTable", null);
//db.execSQL(sql);
//insert(feedbackTable, nullColumnHack, values)
//.rawQuery(sql, null);
result.moveToFirst();
String rfeedback="";
while(!result.isAfterLast()){
rfeedback=result.getString(1);
}
Log.e("returned feedback", "inserted feedback "+rfeedback);
db.close();
Using both execSQL and db.insert my application just hangs waiting for a response. I never reach the Log.e above and because this action takes place in the oncreate method before the screen is drawn, I see only a blank screen. Any idea why?
I suspect that you need to advance your cursor. It would appear that your code is stuck in while (!result.isAfterLast()). Unless you result.moveToNext() (or in some other fashion advance the cursor) it will never be after last and the while statement will loop indefinitely. In fact if you expect only one row you could just eliminate the while loop entirely.