Android SQLite不止一个WHERE

时间:2014-04-08 10:05:48

标签: android sql sqlite where-clause

我一直在寻找它,但我无法找到我需要的东西。我在android中有一个SQLite表,我想知道rawquery如何用于多个WHERE子句。我的意思是,我想知道一个表的所有记录,以及那些两个列的值为1的WHERE的所有列。例如。

我怎么能这样做?

我试过了:

String selectQuery = "SELECT  * FROM " + TABLE_NAME 
        + " WHERE " + KEY_ONE + " = " + String.valueOf(var1)
        + " AND WHERE " + KEY_TWO + " = " + String.valueOf(Var2);

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

但它不起作用..

感谢您的帮助:D

2 个答案:

答案 0 :(得分:3)

删除第二个WHERE

+ " AND " + KEY_TWO + " = " + String.valueOf(Var2);

另请注意,字符串必须用撇号括起来:

String selectQuery = "SELECT  * FROM " + TABLE_NAME 
    + " WHERE " + KEY_ONE + " = '" + String.valueOf(var1)
    + "' AND " + KEY_TWO + " = '" + String.valueOf(Var2) + "'";

如果使用此增强语法,则会更好:

String selectQuery = "SELECT  * FROM " + TABLE_NAME 
    + " WHERE " + KEY_ONE + " = ? AND " + KEY_TWO + " = ?";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(var1), String.valueOf(var2)});

答案 1 :(得分:1)

请尝试以下操作并删除第二个WHERE子句

String selectQuery = "SELECT  * FROM " + TABLE_NAME 
        + " WHERE " + KEY_ONE + " = " + String.valueOf(var1)
        + " AND " + KEY_TWO + " = " + String.valueOf(Var2);

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);