GROUP BY in managedQuery() ?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

GROUP BY in managedQuery() ?

Postby sloth » Sat Jan 02, 2010 7:01 pm

Hey there!

I'm querying the ContactsContract.Data table like the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. [...]
  2.        Uri contactsURI = ContactsContract.Data.CONTENT_URI;
  3.                
  4.                 String[] projection = new String[] {
  5.                                 ContactsContract.Data._ID,
  6.                                 ContactsContract.Data.CONTACT_ID,
  7.                                 ContactsContract.Data.MIMETYPE,
  8.                                 ContactsContract.Data.DATA1,
  9.                                 ContactsContract.Data.DATA2,
  10.                                 ContactsContract.Data.DATA3,
  11.                                 ContactsContract.Data.DATA4,
  12.                                 ContactsContract.Data.DATA5,
  13.                                 ContactsContract.Data.DATA_VERSION
  14.                 };
  15.                 String where =  ContactsContract.Data.MIMETYPE + " = ? OR " +
  16.                                                 ContactsContract.Data.MIMETYPE + " = ? OR " +
  17.                                                 ContactsContract.Data.MIMETYPE + " = ? OR " +
  18.                                                 ContactsContract.Data.MIMETYPE + " = ?";
  19.                
  20.                 String[] whereArgs = new String[] {
  21.                                 ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
  22.                                 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
  23.                                 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
  24.                                 ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
  25.                 };
  26.                
  27.                 Cursor contactsTable = managedQuery(contactsURI, projection, where, whereArgs, null);
  28. [...]
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The problem is that the result is kind of unsorted. It would be best if I could get the rows sorted by MIMETYPE (basically I need the StructuredName to come on top) but grouped for each CONTACT_ID. This means I'd like to get a person's name, then email/phone/im, etc. Then the next person's name and their email/phone/im and so on..

So I thought this would best be achieved if I sorted like Data.MIMETYPE + " DESC" and group by CONTACT_ID. However, there is no possibility to use a group by parameter in a query() or managedQuery(). The only database query I found that does take a group by parameter is the SQLiteQueryBuilder.query(). However, I can't call this one for the ContactsContract.Data Table since it doesn't take an URI but an SQLiteDatabase.

How would you handle the sorting of the data? Should I perform a single query for each CONTACT_ID? Wouldn't that be extremely cost intensive?
sloth
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 01, 2010 11:22 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests