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
  • 22,059
  • 20
  • 123
  • 164
imin
  • 4,504
  • 13
  • 56
  • 103

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
  • 719
  • 9
  • 23
jeet
  • 29,001
  • 6
  • 52
  • 53
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,902
  • 22
  • 27
3

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
  • 484,302
  • 314
  • 1,365
  • 1,393
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
  • 67,150
  • 23
  • 161
  • 242