SQLite where子句问题

时间:2016-04-26 06:09:44

标签: android sqlite where-clause

我在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;

3 个答案:

答案 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)

你可能想试试这个。它改编自我自己的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();使用完毕后。

答案 2 :(得分:-1)

Cursor findEntry = db.query(&#34; sku_table&#34;,columns,&#34; owner =?&#34;,new String [] {owner},null,null,null);