这是我目前的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 });
它返回错误。那我该怎么处理多个?
答案 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
条款使用OR
和WHERE
。如果需要逻辑分组或订单,请使用括号。
SELECT *
FROM employees
WHERE (last_name = 'Smith' AND first_name = 'Jane') OR (employee_id = 1);
注意强>