Search code examples
androiddatabasesqliteandroid-contentprovider

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 ?


Solution

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