16

Here's my current sqlite code:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " + TABLE_RECIPE_NAME + 
                " where " + KEY_ownerID + " = ? ", new String[] { ownerID});

It works fine, but when I tried adding multiple where to something like this:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " + TABLE_RECIPE_NAME + 
                " where " + KEY_ownerID + " = ?, " + KEY_partnerID + " = ?, " + KEY_advertiserID + " = ?, " + KEY_chefID + " = ?", new String[] { ownerID, partnerID, advertiserID, chefID });

It returns an error. So how do I deal with multiple ?

Jav_Rock
  • 21,667
  • 19
  • 119
  • 164
imin
  • 4,206
  • 12
  • 51
  • 88

4 Answers4

47

change query to:

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID from " +
TABLE_RECIPE_NAME + " where " + KEY_ownerID + " = ? AND " + KEY_partnerID +
" = ? AND  " + KEY_advertiserID + " = ? AND " + KEY_chefID + " = ?",
new String[] { ownerID, partnerID, advertiserID, chefID });
Chaitanya
  • 647
  • 10
  • 22
jeet
  • 28,501
  • 6
  • 50
  • 52
6

You are doing correct but just need to use AND or OR keyword between each where condition as per your requirement. So try using this once.

Kailas Bhakade
  • 1,852
  • 22
  • 26
2

Probably you have to use AND. You want a query that should return the value in Cursor populated with comparing multiple values. So, you just need to use AND. Instead of using a comma(,). Above answer seems correct but it just lacks the explanation.

Lalit Poptani
  • 66,608
  • 21
  • 157
  • 241
1

Use AND and OR for multiple WHERE clauses. Use parentheses if logical grouping or order is needed.

SELECT *
FROM employees
WHERE (last_name = 'Smith' AND first_name = 'Jane') OR (employee_id = 1); 

(source)

Note

  • This supplemental answer omits the Android code formatting because it makes the SQL hard to read and understand. The reader can do their own formatting.
Suragch
  • 428,106
  • 278
  • 1,284
  • 1,317