9

I am trying to execute the following query in my android app:

private static final String[] PROJECTION =
            {
                    Data.CONTACT_ID, Data.MIMETYPE,
                    Data.DISPLAY_NAME, Phone.NUMBER,
                    Phone.TYPE, StructuredName.GIVEN_NAME,
                    StructuredName.MIDDLE_NAME,
                    StructuredName.FAMILY_NAME,
                    Data.DELETED
            };


private static final String SELECTION = "(" + Data.MIMETYPE + " = ? AND "
        + Phone.TYPE + " IN (?,?,?)) OR ("
        + Data.MIMETYPE + " IN (?))";


private static final String[] SELECTION_ARGS =
        {
                Phone.CONTENT_ITEM_TYPE,
                String.valueOf(Phone.TYPE_MOBILE),
                String.valueOf(Phone.TYPE_HOME),
                String.valueOf(Phone.TYPE_WORK),
                StructuredName.CONTENT_ITEM_TYPE
        };
final Cursor cursor = mContext.getContentResolver().query(
                Data.CONTENT_URI, PROJECTION, SELECTION, SELECTION_ARGS, Data.CONTACT_ID);

But I get the following error:

04-29 11:00:58.715  8588 15565 E DatabaseUtils: Writing exception to parcel 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils: java.lang.IllegalArgumentException: Invalid column deleted 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:632) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:447) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:387) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at com.android.providers.contacts.ContactsProvider2.doQuery(ContactsProvider2.java:6528) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6479) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:5038) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.content.ContentProvider$Transport.query(ContentProvider.java:238) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 04-29 11:00:58.715  8588 15565 E 
DatabaseUtils:  at android.os.Binder.execTransact(Binder.java:453)

I want to know if the contact that I am reading is deleted or not, so the Data.DELETED column is needed for me and that seems to be causing the error. But when I checked the Android documentation, DELETED does seem to be a valid column to use with ContactsContract.Data.

Rashid
  • 1,700
  • 1
  • 23
  • 56
somesh
  • 2,509
  • 3
  • 16
  • 24
  • It looks like based on that documentation that you must "join" the ContactsContract.RawContacts table together with the ContactsContract.Contacts table in order to gain access to those additional fields – Harvtronix May 02 '16 at 03:34
  • From what I have seen so far, I can see that joins can be done on custom tables. But since the Contacts table is accessed through content resolver, I am not sure how I can do a join. – somesh May 02 '16 at 03:37
  • Though I am not personally familiar with it, are you able to try a "raw query" as suggested here? http://stackoverflow.com/questions/4957009/how-do-i-join-two-sqlite-tables-in-my-android-application – Harvtronix May 02 '16 at 03:39
  • Well, I don't think the table names for the contacts database are exposed outside. With the content resolver query, I'll just be passing in a URI. Don't think I can do joins as mentioned in that post since the table names are not known. – somesh May 02 '16 at 03:46
  • okay i think maybe i have figured it out? rather than relying on Data.CONTACT_ID try instead using Data.RAW_CONTACT_ID. My hope is that this will cause an "implicit join" against the desired RawContacts table. I don't have a means by which to test this myself, but I'm just trying to help you wade through the docs. – Harvtronix May 02 '16 at 03:56
  • Also perhaps check out the "Query" section on this page for its example code: http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html – Harvtronix May 02 '16 at 04:00
  • 1
    Please take a look at this: http://stackoverflow.com/questions/30609515/how-do-implicit-joined-columns-work-with-android-contacts-data – christian mini May 08 '16 at 17:09

1 Answers1

0

Changing the URI to ContactsContract.RawContactsEntity.CONTENT_URI as mentioned in How do implicit joined columns work with Android contacts data? seemed to work.

Community
  • 1
  • 1
somesh
  • 2,509
  • 3
  • 16
  • 24