Question

I have been looking for it but I am unable to find what I need. I have a SQLite table in android, and I would like to know how the rawquery would be for more than one WHERE clause. I mean, I want to know all the records of a table and all columns of those WHERE the value of two colums are 1 for example.

How could I do this?

I've tried:

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);

But it isn't working..

Thanks for the help :D

Was it helpful?

Solution

Remove the second WHERE:

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

Also note that strings must be enclosed in apostrophes:

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

Even better if you use this enhanced syntax:

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)});

OTHER TIPS

Please try as follows and remove the second WHERE clause

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top