2012-01-30 45 views
15

這裏是我當前的SQLite代碼:的Android 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 }); 

它返回一個錯誤。那麼我該如何處理多重?

回答

36

變化查詢:

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

也許你不得不使用AND。你想要一個查詢,該查詢應該通過比較多個值來返回Cursor中的值。所以,你只需要使用AND。而不是使用comma(,)。上面的答案似乎是正確的,但它只是缺乏解釋。

0

使用ANDOR多個WHERE條款。如果需要邏輯分組或順序,請使用括號。

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

(source)

注意

  • 這個補充回答省略了Android的代碼格式,因爲它使SQL難以閱讀和理解。讀者可以做他們自己的格式。
2

你是做正確的,但只需要使用各間OR關鍵字where條件按您的要求。所以請嘗試使用一次。