如何通过“group by”向媒体提供商查询选项?

发布于 2024-09-03 20:28:37 字数 724 浏览 4 评论 0原文

我是 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;

我尝试设置 projectionwhere 如下:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我闹别瞎闹 2024-09-10 20:28:37
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); 

参考页面 = http://zengyan2012.iteye.com/blog/1118963

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

雾里花 2024-09-10 20:28:37

您不能从 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 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.

攀登最高峰 2024-09-10 20:28:37

我不确定这是否可能。

我最近在处理类似的问题上遇到了困难,我设法通过将 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 the GROUP BY option, and therefore they left it out. You also can't suppose always that count(_id) will work.

梦旅人picnic 2024-09-10 20:28:37

首先请原谅我糟糕的英语!
我是 Java/Android 新手,从 4.2.1 开始,并与之斗争了近 2 天,然后我开始阅读有关 SQLiteQueryBuilder query 部分几乎就是您正在寻找的内容;)

它具有:

public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)

内容的查询“功能”提供者只给你:

query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)

在这里你可以欺骗,我将向你发布我的代码片段:

    @Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
/* a String is a Object, so it can be null!*/
    String groupBy = null;
    String having = null;

    switch (sUriMatcher.match(uri)) {
...
...
...
        case EPISODES_NEXT:
        groupBy = "ShowID";
        queryBuilder.setTables(EpisodenTable.TableName);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
            groupBy, having, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

就是这样!

这是我用来执行的代码:

        Cursor showsc = getContext().getContentResolver().query(
            WhatsOnTVProvider.CONTENT_EPISODES_NEXT_URI,
            EpisodenTable.allColums_inclCount,
            String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000)
                    + " < date", null, null);

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:

public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)

the query "function" of the Content Provider only gives you:

query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)

here u can trick around, i will post you my code snip:

    @Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
/* a String is a Object, so it can be null!*/
    String groupBy = null;
    String having = null;

    switch (sUriMatcher.match(uri)) {
...
...
...
        case EPISODES_NEXT:
        groupBy = "ShowID";
        queryBuilder.setTables(EpisodenTable.TableName);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
            groupBy, having, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

thats its!

here the code i use to execute:

        Cursor showsc = getContext().getContentResolver().query(
            WhatsOnTVProvider.CONTENT_EPISODES_NEXT_URI,
            EpisodenTable.allColums_inclCount,
            String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000)
                    + " < date", null, null);
深爱成瘾 2024-09-10 20:28:37

这种方法对我有用:
(我找到了解决方案此处

val projection = arrayOf(
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.MediaColumns.DATA
    )
    val selection = " 1=1 ) GROUP BY (${MediaStore.Images.Media.BUCKET_DISPLAY_NAME}"
    val cursor = contentResolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null
    )

This approach worked for me:
(I have found the solution here)

val projection = arrayOf(
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.MediaColumns.DATA
    )
    val selection = " 1=1 ) GROUP BY (${MediaStore.Images.Media.BUCKET_DISPLAY_NAME}"
    val cursor = contentResolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文