Great CONTACT_ID from ContactsContract.Data

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?

+4
source share
1 answer

If you target devices below ICS, you can use the GROUP_BY clause by adding ) before the group and ( after:

 selectionBuilder.append(") GROUP BY (") 

As with ICS and above, the query interpreter is smarter and closes any closed brackets to prevent insertion.

However, I do not understand why separate contact_ids are needed here. Perhaps a contact should have only one Data to associate it with one group, so you are likely to get another contact on each line.

In addition, something may be related to http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_GROUP_URI , it is not documented, but, given its position, there may well be direct access to contacts belonging to the group. You would use this Uri:

 Uri uri = ContentUri.withAppendedId(ContactsContract.Contacts.CONTENT_GROUP_URI, groupId); 

And then request it as Contacts.CONTENT_URI

+4
source

Source: https://habr.com/ru/post/1400583/


All Articles