我想从我使用我的应用程序创建的数据库中获取一些数据。这是我的代码
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.select_to_delete);
database = new DatabaseHandler(this);
bundle = getIntent().getExtras();
date = bundle.getString("DATE");
SQLiteDatabase db=database.getWritableDatabase();
Cursor cur=db.rawQuery("SELECT " +TITLE+ " FROM " +TABLE_NAME + " WHERE " + CDATE + "=" + date,null);
int i = cur.getCount();
if(cur !=null){
if (cur.moveToFirst()){
do{
tdate = cur.getString(cur.getColumnIndex("DATE"));
titlearray.add(tdate);
}while(cur.moveToNext());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titlearray);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
我的表有5列(id,title,date,time,descrption)。并有4个原料。 该表在另一个类中创建。
当我运行此应用程序时,它不会从sql表中获取数据并在listview中显示它。当我调试它时,它显示i=1
并且它返回false给if (cur.moveToFirst())
。所以它不会进入if
部分。
我的错误是什么?
答案 0 :(得分:1)
将撇号(')放在你的日期附近
Cursor cur=db.rawQuery("SELECT " +TITLE+ " FROM " +TABLE_NAME + " WHERE " + CDATE + "='" + date+"'",null);
N.B。我假设您的表创建和其他字段名称是准确的。还要重新检查那些
答案 1 :(得分:1)
使用选择参数,您将避免大量问题:
Cursor cur=db.rawQuery("SELECT " +TITLE+ " FROM " +TABLE_NAME + " WHERE " + CDATE + " = ?", new String[] { date });
答案 2 :(得分:0)
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { TITLE}, CDATE + "=?",
new String[] { String.valueOf(date) }, null, null, null, null);
if (cursor.moveToFirst()) {
do {
//add to the array
} while (cursor.moveToNext());
}