0

如何向 SQLliteQueryBuilder 查询添加多个选择(where 子句)?例如,(年龄在 10 到 20 岁之间且身高 = 60)或(性别 = 'F')

public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)

selection   A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URL.
selectionArgs   You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
4

1 回答 1

10

基本上你在selection字符串中传递条件,你提到像

String selection = "arg1 = ? and arg2 = ?" ...

selectionArgsis String 数组,所以这里给出了选择值:

selectionArgs[0] = "value of arg1";
selectionArgs[1] = "value of arg2";

and so on

所以你可以使用:

String selection = "Age between ? and ? and Height = ?";
selectionArgs[0] = "10";
selectionArgs[1] = "20";
selectionArgs[2] = "168";

然后将这些字符串传递给query.

希望这可以帮助!

于 2012-05-20T19:38:22.013 回答