简体   繁体   中英

Android Content Provider how to query with multiple selectionArg

I have table like below:

Students:

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

I want to query only students which have id 1 and 3 .

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

what I have to write in selectionArg so i only get students with id 1 and 3 ?

The answer i found is to add id in (?,?) for "selection" and give "selectionArgs[]" to query. ? signs replaced with elements inside "selectionArgs[]". You have to add ? for each element so i did this:

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);

have you tried

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

or if you use placeholders "?" selectionArg must contain values "1" and "3"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM