如何获取内容提供商中的 _count?

发布于 2024-07-11 14:25:47 字数 89 浏览 5 评论 0原文

我应该怎么做才能让我的内容提供商返回包含记录数的 _count 列? 文档说它是自动的,但也许它只需要一些内置的内容提供程序。 对数据库运行查询似乎不会返回它。

What should I do to get my content provider to return the _count column with the count of records? The documentation says it is automatic, but maybe it's only taking about some built-in content provider. Running a query to the database seems not to return it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你与清晨阳光 2024-07-18 14:25:47

如果您使用的是 contentProvider,那么您必须像 count(*) AS count 那样执行。

如果您使用cursor.getCount(),则效率不如上述方法。 使用cursor.getCount(),您可以获取所有记录以获取计数。 整个代码应该如下所示 -

 Cursor countCursor = getContentResolver().query(CONTENT_URI,
                new String[] {"count(*) AS count"},
                null,
                null,
                null);

        countCursor.moveToFirst();
        int count = countCursor.getInt(0);

之所以有效,是因为 android 需要定义一个列名。

If you are using contentProvider then you have to do it like count(*) AS count.

If you use cursor.getCount(), that would not be as efficient as the above approach. With cursor.getCount() you are fetching all the records just to get counts. The entire code should look like following -

 Cursor countCursor = getContentResolver().query(CONTENT_URI,
                new String[] {"count(*) AS count"},
                null,
                null,
                null);

        countCursor.moveToFirst();
        int count = countCursor.getInt(0);

The reason why this works is because android needs a column name to be defined.

笑红尘 2024-07-18 14:25:47

如果您使用 ContentProvider.query(),则会返回 Cursor。 调用 Cursor.getCount() 来获取返回游标中的记录计数。

If you are using ContentProvider.query() a Cursor is returned. Call Cursor.getCount() to get a count of records in the returned cursor.

深爱成瘾 2024-07-18 14:25:47

我遇到了类似的问题,发现这对我有用。 在下面的示例中,我想从 MediaStore 提供商获取图像的数量。

        final String[] imageCountProjection = new String[] {
                "count(" + MediaStore.Images.ImageColumns._ID + ")",
        };

        Cursor countCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageCountProjection,
                null,
                null,
                null);

        countCursor.moveToFirst();
        int existingImageCount = countCursor.getInt(0);

I had a similiar problem and found this worked for me. In the example below I wanted to get the count of images from the MediaStore provider.

        final String[] imageCountProjection = new String[] {
                "count(" + MediaStore.Images.ImageColumns._ID + ")",
        };

        Cursor countCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                imageCountProjection,
                null,
                null,
                null);

        countCursor.moveToFirst();
        int existingImageCount = countCursor.getInt(0);
不打扰别人 2024-07-18 14:25:47

使用cursor.getCount(),您无法确保它返回所返回项目的真实数量。 还有很多更好的方法:

1- 如果您使用内容提供程序,则可以执行查询并使用 BaseColumns 中包含的列 (_COUNT) 进行投影

@Override
public Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    ...

    projection = new String[] {
        ContentContract.NotificationCursor.NotificationColumns._COUNT,
    };

    ...

    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);
    return cursor;
}

2- 使用 执行 rawQuery SELECT COUNT(*) 正如 @saurabh 在他的回复中所说。

With cursor.getCount() you can not assure that it returns the real number of items returned. There are much better ways:

1- If you are using Content Providers, you can do a query and use the Column (_COUNT) included in BaseColumns for your projection

@Override
public Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    ...

    projection = new String[] {
        ContentContract.NotificationCursor.NotificationColumns._COUNT,
    };

    ...

    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);
    return cursor;
}

2- To do a rawQuery using SELECT COUNT(*) as @saurabh says in his response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文