Android Content Provider如何使用多个selectionArg进行查询

时间:2015-03-19 10:56:15

标签: android database sqlite android-contentprovider

我有如下表:

生:

id   |  student_name
------------------
1    |  max
2    |  alex
3    |  james

我只想查询ID为13的学生。

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=?",selectionArg,null);

我必须在selectionArg写一下,所以我只能得到身份1和3的学生?

2 个答案:

答案 0 :(得分:2)

我找到的答案是为"选择"添加id in (?,?)并给予" selectionArgs []"查询。 ?符号替换为" selectionArgs []"内的元素。你必须为每个元素添加?,所以我这样做了:

String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
    selection1 += "?, ";}           
selection1 = selection1.substring(0, selection1.length() - 2) + ")";            
getContentResolver().query(StudentContentProvider.CONTENT_URI1, projection1, selection1, selectionArgs1, null);

答案 1 :(得分:1)

你试过吗

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=1 or id=3",null,null);

或者如果你使用占位符“?” selectionArg必须包含值“1”和“3”

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=? or id=?",selectionArg,null);