SQLite where 子句问题
SQLite where clause Issue
我在 SQLite table 中有大约 30
条记录,其中 10
条记录属于 在线 的来源,其余 20
记录属于来源 Offline.
现在,我想从 Online
中获取属于 source 的所有 records
,为此我写了这个:
String sQuery = "SELECT * FROM " + TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'";
但是通过使用上面的查询,我得到了数据库中可用的所有记录,无论它属于离线还是在线
所以我错了,为什么我无法获取仅属于源“Online
”的数据
而,我使用下面的查询从属于 source 'Online'
的 table 到 delete data
,它对我有用:
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;
}
但仍然获取所有记录,而我只想获取属于源的记录'Online'
试试这个方法:
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);
游标 findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);
你可能想试试这个。它改编自我自己的 sqlite 代码。希望对你有帮助。
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();使用完毕后。
我在 SQLite table 中有大约 30
条记录,其中 10
条记录属于 在线 的来源,其余 20
记录属于来源 Offline.
现在,我想从 Online
中获取属于 source 的所有 records
,为此我写了这个:
String sQuery = "SELECT * FROM " + TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'";
但是通过使用上面的查询,我得到了数据库中可用的所有记录,无论它属于离线还是在线
所以我错了,为什么我无法获取仅属于源“Online
”的数据
而,我使用下面的查询从属于 source 'Online'
的 table 到 delete data
,它对我有用:
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;
}
但仍然获取所有记录,而我只想获取属于源的记录'Online'
试试这个方法:
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);
游标 findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);
你可能想试试这个。它改编自我自己的 sqlite 代码。希望对你有帮助。
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();使用完毕后。