android sqlite查询多个在哪里

时间:2012-01-30 08:58:29

标签: android sqlite

这是我目前的sqlite代码:

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

它工作正常,但是当我尝试在这样的地方添加多个地方时:

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

它返回错误。那我该怎么处理多个?

4 个答案:

答案 0 :(得分:40)

将查询更改为:

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

答案 1 :(得分:3)

您做得正确,但只需根据您的要求在每个where条件之间使用 AND OR 关键字。所以尝试使用一次。

答案 2 :(得分:1)

可能你必须使用AND。您想要一个查询,该查询应返回Cursor中填充的值,并使用比较多个值。所以,你只需要使用AND。而不是使用comma(,)。上面的答案似乎是正确的,但它只是缺乏解释。

答案 3 :(得分:1)

对多个AND条款使用ORWHERE。如果需要逻辑分组或订单,请使用括号。

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

(source)

注意

  • 此补充答案省略了Android代码格式,因为它使SQL难以阅读和理解。读者可以进行自己的格式化。