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 ?

0

4 Answers 4

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 });
Sign up to request clarification or add additional context in comments.

1 Comment

and what if we are looking same field with three similarity?
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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.