【问题标题】:How to GROUP BY when using a Content Provider?使用内容提供者时如何分组?
【发布时间】:2019-03-27 10:39:07
【问题描述】:

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

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

这是表格:

我想编写查询:SELECT OrderID, sum(quantity) FROM OrderDetails GROUP BY OrderID 它将返回:

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

这里是获取光标并简单打印出我刚刚制作的结果的方法:

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 已被删除且不再受支持。有没有其他方法可以提供相同的结果?

谢谢

【问题讨论】:

  • 如果输出名为 selection 的变量,你会看到什么?

标签: android sql group-by cursor android-contentprovider


【解决方案1】:

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

除非您计划让多个应用都使用这些数据,否则我不建议您实施 ContentProvider

我认为 GROUP BY 已被删除且不再受支持

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

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

ContentProvider 上实现GROUP BY,而不是在客户端上。 IOW,处理这种情况的方式与使用 REST 样式的 Web 服务相同,其中实现 SQL 的是 Web 服务,而不是 Web 服务的客户端。

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

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

由于我们没有您的ContentProvider 实现,我们无法对此发表评论。但是,请注意 GROUP BY 不是您返回的东西,因此将其放入投影中将是一种非典型选择。

【讨论】:

    【解决方案2】:

    回答我自己的问题:

    Stackoverflow 上有答案 - Android: Distinct and GroupBy in ContentResolver 我会留下这个问题,因为已经在回复中考虑了一些想法。

    虽然我觉得,如其他答案中所述,这应该在内容提供程序本身中进行排序。但是,当您调用 Content Provider 时,有一个快速修复方法:

    在您的投影字符串中,您可以简单地添加...

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      不确定您是否仍在寻找答案,但是,GROUP BY with Content Provider 的一个技巧是将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);
          }
      }
      

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

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

      希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 2015-02-28
        • 2012-04-05
        相关资源
        最近更新 更多