I need to query the ContactsContract.Data table, and the values ββin the CONTACT_ID column will be different (different).
the code:
final Uri uri = ContactsContract.Data.CONTENT_URI; final String[] projection = new String[] {// ContactsContract.Data.CONTACT_ID, // ContactsContract.Data._ID, // ContactsContract.Data.DISPLAY_NAME,// ContactsContract.Data.LOOKUP_KEY // }; final StringBuilder selectionBuilder = new StringBuilder(); selectionBuilder.append(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID); selectionBuilder.append("= ? AND "); selectionBuilder.append(ContactsContract.Data.MIMETYPE); selectionBuilder.append("= ? "); final String selection = selectionBuilder.toString(); final String[] selectionArgs = new String[] {// String.valueOf(groupId), // ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE // }; return context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
First of all, I tried to add " DISTINCT " to ContactsContract.Data.CONTACT_ID in the projection. But there was an exception: java.lang.IllegalArgumentException: Invalid column DISTINCT contact_id
Then I write as follows:
"'DISTINCT "+ContactsContract.Data.CONTACT_ID+"'". java.lang.IllegalArgumentException: Invalid column 'DISTINCT contact_id'
Then add to selectionBuilder:
selectionBuilder.append(" GROUP BY ").append(ContactsContract.Data.CONTACT_ID);
Once again, the exception: android.database.sqlite.SQLiteException: next to "GROUP": syntax error: when compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? GROUP BY contact_id) ORDER BY display_name ASC
Finally, I add the "group by" statement right after sortOrder, but:
android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? ) ORDER BY display_name ASC GROUP BY contact_id
Is it possible to make a request clear? Maybe I should add something to the URI?