如何在使用内容提供商时进行GROUP BY?

问题描述 投票:0回答:3

我为我的应用程序创建了一个内容提供程序。为简单起见,我将举一个例子。我有一个表'OrderDetails'。

  1. 我有一个OrderId专栏。
  2. 我有一个列,列出了在OrderId中购买的不同产品类型的数量。
  3. 我有一个OrderDetailId,它是表的主键。

这是表:enter image description here

我想编写查询:SELECT OrderID, sum(quantity) FROM OrderDetails GROUP BY OrderID哪个会返回:enter image description here

但是,我试图在我的内容提供程序的URI中插入我的GROUP BY子句,但它不起作用,因此生成的查询变为:SELECT OrderID, sum(quantity) FROM OrderDetails返回此(所有内容的全部数量和最后一个OrderId):enter image description here

这是获取光标的方法,只是打印出我刚刚创建的结果:

private void findQuantityByOrder(){
    Uri uri = DatabaseContract.CONTENT_URI_ORDER_DETAILS;
    String[] selection = new String[] {DatabaseContract.DatabaseOrderDetails.ORDER_ID,
            "sum("+ DatabaseContract.DatabaseOrderDetails.QUANTITY + ")"
                    +"GROUP BY(" + DatabaseContract.DatabaseOrderDetails.ORDER_ID + ")"};
    String projection = null;
    String sortBy = null;
    String[] args = null;

    Cursor cursor = getContentResolver().query(
            uri,
            selection,
            projection,
            args,
            sortBy);

    for (int i = 0; i < cursor.getCount(); i ++) {
        cursor.moveToPosition(i);
        int orderID = cursor.getInt(cursor.getColumnIndex("orderID"));
        int quantity = cursor.getInt(cursor.getColumnIndex("sum(quantity)"));

        System.out.println("orderId: " + orderID + ". Quanitity: " + quantity);
    }
}

它只打印出所有订单的总和,以及表中的最后一个ID。

我相信GROUP BY已被删除,不再受支持。有没有其他方法可以提供相同的结果?

谢谢

android sql group-by cursor android-contentprovider
3个回答
1
投票

我为我的应用程序创建了一个内容提供程序

除非你打算让几个应用程序都使用这些数据,否则我不建议实现ContentProvider

我相信GROUP BY已被删除,不再受支持

ContentProvider / ContentResolver协议一般不支持SQL,更不用说GROUP BY了。毕竟,不要求使用SQL数据库实现ContentProvider

有没有其他方法可以提供相同的结果?

GROUP BY上实施ContentProvider,而不是在客户端上。 IOW,以与使用REST风格的Web服务相同的方式处理此问题,其中实现SQL的是Web服务,而不是Web服务的客户端。

例如,如果Uri路径为/foo处理基本查询,则/foo/summary可能会实现SUMGROUP BY位。

我试图在我的内容提供程序的URI中插入我的GROUP BY子句,但它不起作用

由于我们没有你的ContentProvider实现,我们无法对此发表评论。但是,请注意GROUP BY不是你返回的东西,所以把它放在投影中将是一个非典型的选择。


0
投票

回答我自己的问题:

Stackoverflow上有答案 - Android: Distinct and GroupBy in ContentResolver我会留下这个问题,因为有些想法已被纳入回复中。

虽然我觉得,如其他答案中所述,这应该在内容提供商本身中进行排序。但是,当您呼叫内容提供商时,可以快速解决问题:

在您的投影字符串中,您只需添加...

 String projection = COLUMN_NAME + "'a_condition' GROUP BY " + COLUMN_NAME" 

希望这可以帮助


0
投票

不确定如果你还在寻找答案,但是,对于带有Content Provider的GROUP BY的hack是在group by中嵌入URI子句作为查询参数,并在内容提供者的查询方法中读取。

因此,典型查询可能看起来像

Uri uri = DatabaseContract.CONTENT_URI_ORDER_DETAILS + "?groupBy=" + DatabaseContract.DatabaseOrderDetails.ORDER_ID;

整件事看起来像这样:

private void findQuantityByOrder(){
    Uri uri = DatabaseContract.CONTENT_URI_ORDER_DETAILS + "?groupBy=" + DatabaseContract.DatabaseOrderDetails.ORDER_ID;
    String[] projection = new String[] {DatabaseContract.DatabaseOrderDetails.ORDER_ID,
            "sum("+ DatabaseContract.DatabaseOrderDetails.QUANTITY + ")"};
    String selection = null;
    String sortBy = null;
    String[] args = null;

    Cursor cursor = getContentResolver().query(
            uri,
            projection,
            selection,
            args,
            sortBy);

    for (int i = 0; i < cursor.getCount(); i ++) {
        cursor.moveToPosition(i);
        int orderID = cursor.getInt(cursor.getColumnIndex("orderID"));
        int quantity = cursor.getInt(cursor.getColumnIndex("sum(quantity)"));

        System.out.println("orderId: " + orderID + ". Quanitity: " + quantity);
    }
}

在您的内容提供商代码中,您可以像这样轻松地获取组,

String groupBy = uri.getQueryParameter("groupBy");

希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.