我在SQLite表中有大约30
条记录,其中10
条记录属于来自在线的来源,其余20
条记录属于来自的来源离线
现在,我想从records
获取属于来源的所有Online
,因为我写了这个:
String sQuery = "SELECT * FROM " + TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'";
但是通过使用上述查询,我获取数据库中的所有记录,无论它属于脱机还是在线
所以我错了,为什么我无法获取属于源的数据' Online
'仅
然而,我在delete data
的表中使用以下查询source 'Online'
并且它对我有效:
db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'");
代码
public List<Reminder> getAllOnlineReminders(){
// String selectQuery = "SELECT * FROM " + TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'";
String[] args = new String[]{"Online"};
List<Reminder> reminderList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_REMINDERS + " WHERE " + KEY_SOURCE + "=?", args);
// Cursor cursor = db.rawQuery(selectQuery, null);
// Looping through all rows and adding to list
if(cursor.moveToFirst()){
do{
Reminder reminder = new Reminder();
reminder.setID(Integer.parseInt(cursor.getString(0)));
reminder.setTitle(cursor.getString(1));
reminder.setmSource(cursor.getString(2));
// Adding Reminders to list
reminderList.add(reminder);
} while (cursor.moveToNext());
}
return reminderList;
}
但仍然获得所有记录,而我只想获得属于来源的记录&#39; Online&#39;
答案 0 :(得分:0)
尝试这种方式:
String[] args = new String[]{"Online"};
// For select
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_REMINDERS + " WHERE " + KEY_SOURCE + "=?", args);
// For delete
Cursor cursor = db.rawQuery("DELETE FROM " + TABLE_REMINDERS + " WHERE " + KEY_SOURCE + "=?", args);
答案 1 :(得分:0)
private final String[] allColumns = {"id", "title", KEY_SOURCE};
//or whatever your column name is
String[] args = {"Online"};
Cursor cursor =
db.query(TABLE_REMINDERS, // a. table
allColumns, // b. column names
KEY_SOURCE + "=?", // c. selection
args, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
重要提示:与您的问题无关,但请记得调用cursor.close();使用完毕后。
答案 2 :(得分:-1)
Cursor findEntry = db.query(&#34; sku_table&#34;,columns,&#34; owner =?&#34;,new String [] {owner},null,null,null);