Question

I am getting error in using following query. how to resolve it?

String projection[] = {Data.CONTACT_ID,Data.DISPLAY_NAME,Phone.NUMBER,Data.RAW_CONTACT_ID,Email.DATA,Phone.DATA1,StructuredPostal.STREET,StructuredPostal.POSTCODE};

        Cursor c = getContentResolver().query(
                Data.CONTENT_URI, 
                projection, 
                "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) GROUP BY ?", 
                new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE, Data.CONTACT_ID},
                Data.CONTACT_ID);

Logcat :

02-06 14:18:01.175: E/AndroidRuntime(6884): FATAL EXCEPTION: IntentService[ContactManagerService]
02-06 14:18:01.175: E/AndroidRuntime(6884): android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, display_name, data1, raw_contact_id, data1, data1, data4, data9 FROM view_data data WHERE (1) AND (((mimetype=? OR mimetype=?) GROUP BY ?)) ORDER BY contact_id
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.content.ContentResolver.query(ContentResolver.java:311)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at com.ecosmob.contactpro.service.ContactManagerService.onHandleIntent(ContactManagerService.java:103)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.os.Looper.loop(Looper.java:137)
02-06 14:18:01.175: E/AndroidRuntime(6884):     at android.os.HandlerThread.run(HandlerThread.java:60)
Was it helpful?

Solution

It's not really possible to use GROUP BY with a ContentResolver query. There used to be some parenthesis injection tricks to place the GROUP BY in a syntactically correct place but that doesn't work on all platform versions.

Also, you can use ? placeholder for literals only, not for identifiers such as column names.

Consider another approach to get the data you need in a way you want.

OTHER TIPS

Maybe I'm too late in answering the query but I got a turnaround for this. The selection in the ContentResolver query is a where clause and could be used for GROUP BY after closing it.

String selection = "(1) GROUP BY" COLUMN_NAME";
Cursor c = getContentResolver().query(uri,projection,selection,null,null);

This will group the cursor by COLUMN_NAME.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top