android sqlite查询多个在哪里

问题描述 投票:15回答:4

这是我目前的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 });

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

android sqlite
4个回答
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 });

3
投票

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


1
投票

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


1
投票

使用ANDOR作为多个WHERE条款。如果需要逻辑分组或订单,请使用括号。

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

(source)

注意

  • 此补充答案省略了Android代码格式,因为它使SQL难以阅读和理解。读者可以进行自己的格式化。
© www.soinside.com 2019 - 2024. All rights reserved.