我有一个数据库,我想从中查询并仅显示具有一种类似字段的数据。 假设所有行都有一天(星期一到星期日)...我想显示星期一作为日期的所有行... 我使用SimpleCursorAdapter来显示数据库中的所有行... 基本上,我想知道我如何在SimpleCursorAdapter中查询显示只有星期一作为日期的数据行...
感谢任何帮助...... :)
以下代码使我能够显示数据库中的所有数据......
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout2);
listContent = (ListView)findViewById(android.R.id.list);//.id..list);
mDB = new DataSource(this);
mDB.open();
cursor = mDB.queueAll();
String[] from = new String[]{
MySQLiteHelper.KEY_START, MySQLiteHelper.KEY_END,
MySQLiteHelper.KEY_SUBJECT, MySQLiteHelper.KEY_LOC,
MySQLiteHelper.KEY_TEACHER };
int[] to = new int[] {R.id.tvStart, R.id.tvEnd,
R.id.tvSub, R.id.tvLoc, R.id.tvTeacher };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor,
from, to);
listContent.setAdapter(cursorAdapter);
listContent.setOnItemClickListener(listContentOnItemClickListener);
}
}
我希望查询像这样..
Cursor cursor = db.query(table, null, "day like " + "'%Monday%'",null,
null, null, null);
得到了......抱歉这件事。请告诉我,虽然我错在哪里.. 我在代码中做了这个来查询日期并按时间升序排序..
public Cursor queryDay(String days) {
Cursor cursor = mDatabase.query(DATABASE_TABLE, mAllColumns, "day like " +
days, null, null, null, Database.KEY_START + " ASC");
if (cursor.moveToNext()) {
return cursor;
}
return cursor;
}
但是,我收到此错误,
android.database.sqlite.SQLiteException: no such column: Thursday: ,
while compiling: SELECT _id, day,start_time, end_time, sub, location,
teacher FROM subjectTable WHERE day like Thursday ORDER BY start_time ASC
我这样称呼这个......
Cursor cursor = mDB.queryDay("Thursday");
startManagingCursor(cursor);
答案 0 :(得分:2)
您不查询适配器,查询数据库,它提供了一个光标,您将其注入列表适配器。
mDbHelper.rawQuery("SELECT * FROM your_table WHERE day_column = 'Monday');
答案 1 :(得分:0)
这解决了问题的第二部分..
public Cursor queryDay(String days) {
Cursor cursor = mDatabase.query(DATABASE_TABLE, mAllColumns, "day like "
+ "'" + days + "'", null,null, null, Database.KEY_START + " ASC");
if (cursor.moveToNext()) {
return cursor;
}
return cursor;
}