这是我目前的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 });
它返回一个错误。那我该怎么处理多个?
将查询更改为:
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 });
你做得对,但只需要根据你的要求在每个where条件之间使用AND或OR关键字。所以尝试使用一次。
可能你必须使用AND
。您想要一个查询,该查询应返回Cursor中填充的值,并使用比较多个值。所以,你只需要使用AND
。而不是使用comma(,)
。上面的答案似乎是正确的,但它只是缺乏解释。
使用AND
和OR
作为多个WHERE
条款。如果需要逻辑分组或订单,请使用括号。
SELECT *
FROM employees
WHERE (last_name = 'Smith' AND first_name = 'Jane') OR (employee_id = 1);
注意