Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query from MEDIA provider with "group by" option?

I'm a newbie to Android. Actually, I want to query data from Media provider with Content provider & content resolver.

c = mContent.query(CONTENT_URI,projection,where,null,null); 

My question is, how can I query data from media provider as below using a GROUP BY clause:

select DISTINCT _id, count(_id), _data FROM aaa_table WHERE _data LIKE "A" OR _data LIKE "B" GROUP BY _id;

I have tried setting projection and where as follows:

 final String[] projection = new String[] {
                "_id", 
                "COUNT ("+ _id +")" ,
                "_data" 
                }; 

and where:

_data LIKE "A" OR _data LIKE "B"

but, I couldn't find how to set the query option GROUP BY _id.

Please help me.

like image 872
gkshope Avatar asked Jun 03 '10 10:06

gkshope


2 Answers

where = "_data LIKE 'A' OR _data LIKE 'B'";
where += ") GROUP BY (_id"; // note the char ')' and '(', the ContentResover will completed for U
c = mContent.query(CONTENT_URI,projection,where,null,null); 

referance page = http://zengyan2012.iteye.com/blog/1118963

like image 168
Shuai Avatar answered Oct 06 '22 07:10

Shuai


You can't from a ContentProvider. Now, if you're writing your ContentProvider you could implement it. Inside your content provider you'd have to use a SQLiteQueryBuilder class which has a query() method that takes a GROUP BY string.

http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html

This class also has a setDistinct(true) method that sets the query as DISTINCT, as you indicated you require in your SQL statement.

like image 40
Ricardo Villamil Avatar answered Oct 06 '22 07:10

Ricardo Villamil