任何人都可以看看我附加的代码,并告诉我为什么我没有插入或从数据库中选择任何值。我什至不知道我没有做什么,因为我没有得到任何错误。我所知道的是,当我运行select查询时,光标不包含任何内容。Android SQLite数据库插入/选择不工作
感谢
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();
第二期上面的代码改变后:
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();
同时使用execSQL和db.insert我的应用程序只是挂起等待响应。我从来没有达到上面的Log.e,因为这个动作发生在绘制屏幕之前的oncreate方法中,所以我只看到一个空白屏幕。任何想法为什么?
查看[本] [1]帖子。尝试使用db.insert()而不是db.rawQuery()。 [1]:http://stackoverflow.com/questions/2616130/android-insert-into-sqlite-database – Jack
是的,我看到 - I前人的精力已经使用execSQL或db.insert。我已经尝试过,现在都有一个新的问题,所以也必须有第二个问题。 –
摆脱您的额外引号。您不应该需要将“值”插入到字符串中。要么根本不包围,要么使用单引号“' – Jack