如何通过“group by”向媒体提供商查询选项?
我是 Android 新手。 实际上,我想通过内容提供商和媒体提供商查询数据。内容解析器。
c = mContent.query(CONTENT_URI,projection,where,null,null);
我的问题是,如何使用 GROUP BY
子句从媒体提供商查询数据,如下所示:
select DISTINCT _id, count(_id), _data FROM aaa_table WHERE _data LIKE "A" OR _data LIKE "B" GROUP BY _id;
我尝试设置 projection
和 where
如下:
final String[] projection = new String[] {
"_id",
"COUNT ("+ _id +")" ,
"_data"
};
和where
:
_data LIKE "A" OR _data LIKE "B"
但是,我找不到如何设置查询选项GROUP BY _id
。
请帮我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
参考页面 = http://zengyan2012.iteye.com/blog/1118963
referance page = http://zengyan2012.iteye.com/blog/1118963
您不能从
ContentProvider
获取。现在,如果您正在编写ContentProvider
,则可以实现它。在内容提供程序中,您必须使用SQLiteQueryBuilder
类,该类具有采用GROUP BY
字符串的query()
方法。http://developer.android.com/reference/android/database/ sqlite/SQLiteQueryBuilder.html
该类还有一个
setDistinct(true)
方法,用于将查询设置为 DISTINCT,正如您在 SQL 语句中所指出的那样。You can't from a
ContentProvider
. Now, if you're writing yourContentProvider
you could implement it. Inside your content provider you'd have to use aSQLiteQueryBuilder
class which has aquery()
method that takes aGROUP 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.我不确定这是否可能。
我最近在处理类似的问题上遇到了困难,我设法通过将
ContentProvider
中的数据插入到临时表中并查询表中的结果来解决此问题。故事是
ContentProvider
背后的数据可能不是数据库。它可以是 XML、JSON、文件系统等...因此这些没有 GROUP BY 选项,因此他们将其排除在外。您也不能假设count(_id)
总是有效。I am not sure if that possible.
I was struggling with similar stuff lately, and I managed to workaround by inserting the data from
ContentProvider
into a temporary table and querying the table for results.The story is that the data behind a
ContentProvider
might not be a database. It can be XML, JSON, FileSystem etc... So these do not have theGROUP BY
option, and therefore they left it out. You also can't suppose always thatcount(_id)
will work.首先请原谅我糟糕的英语!
我是 Java/Android 新手,从 4.2.1 开始,并与之斗争了近 2 天,然后我开始阅读有关 SQLiteQueryBuilder query 部分几乎就是您正在寻找的内容;)
它具有:
内容的查询“功能”提供者只给你:
在这里你可以欺骗,我将向你发布我的代码片段:
就是这样!
这是我用来执行的代码:
first off all excuse my POOR English!
I'm new to Java/Android, started with 4.2.1 and fight with that too almost 2 days, then i start reading some more details about SQLiteQueryBuilder the query part is pretty much that what u are looking for ;)
it have:
the query "function" of the Content Provider only gives you:
here u can trick around, i will post you my code snip:
thats its!
here the code i use to execute:
这种方法对我有用:
(我找到了解决方案此处)
This approach worked for me:
(I have found the solution here)