gpt4 book ai didi

android - Room 中的 Sum 和 Count - 别名没有这样的列

转载 作者:行者123 更新时间:2023-11-29 01:01:29 26 4
gpt4 key购买 nike

所以我最近跳进了房间,我面临着一个关于别名的奇怪问题。

我将简化这些类,以便我的示例更加清晰。假设我有包含一些产品的账单。

我想获取一段时间内(天、小时、...)的账单列表及其产品。

我需要实体 Bill 和 Product :

Entity Bill.java

@Entity  
public class Bill{
@PrimaryKey(autoGenerate = true)
int id;
Date date;
}

Entity Product.java

@Entity  
public class Product{
@PrimaryKey(autoGenerate = true)
int id;
int bill_id;
BigDecimal price;
String name;
}

我有一个 POJO,代表一张包含其产品的账单。

POJO BillWithProducts.java :

public class BillWithProducts{
@Embedded
public Bill bill;

@Relation(parentColumn = "id", entityColumn = "bill_id")
public List<Product> products;
}

以及用于查询的 Daos :

Dao BillDao.java

@Dao  
public abstract class BillDao{
@Query("select * from bill")
public abstract List<Bill> getAllBills();

@Transaction
@Query("select * from bill where date between :startDate and :endDate")
public abstract List<BillWithProducts> getBillsWithProductsByDate(long startDate, long endDate);
}

Dao ProductDao.java :

@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();
}

到目前为止一切顺利。但是对于显示偏好,我希望在一行中使用相同名称的产品,并相应地添加它们的数量和价格。

为此目的(并且因为我的 Product.java 有很多这里不需要的信息;保持最小化),我创建了我的 Product 类的 POJO。

POJO ProductMinimal.java :

public class ProductMinimal{
int id;
int bill_id;
BigDecimal price;
String name;
int quantity;
BigDecimal sumPrices;
}

我编辑了 BillWithProducts.java 中的关系以获得 ProductMinimal 的列表。

Edited POJO BillWithProducts.java :

public class BillWithProducts{  
@Embedded
Bill bill;

@Relation(parentColumn = "id", entityColumn = "bill_id", entity = Product.class)
public List<ProductMinimal> productsMinimal; //<-- error pointing here
}

并在 ProductDao.java 中添加查询。

Edited dao ProductDao.java :

@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();

@Query("select id, bill_id, price, name, " +
"count(*) as quantity, sum(price) as sumPrices " +
"from product " +
"group by name")
public abstract List<ProductMinimal> getAllProductsMinimal();
}

最后,我来这里的原因是我得到了指向 BillWithProducts.java 中的关系的错误...

error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such column: quantity)

当然没有这样的栏目,是别名...

我试过了:
- 为别名添加简单引号,例如“count(*) as 'quantity'”
- 删除计数(*);错误转到 sumPrices
- 将 Product 嵌入到 ProductPojo 中(因此具有“select *”和许多无用的字段)
- 我什至尝试使用 Product 的现有列作为别名...

你们中有人遇到过类似的情况吗?

编辑:我尝试在该数量别名中获取 bill_id,例如:

@Query("select id, bill_id, price, name, " +  
"bill_id as quantity " +
"from product " +
"group by name")
public abstract List<ProductMinimal> getAllProductsMinimal();

仍然没有这样的列错误,因此它没有链接到求和或计数操作。

EDIT2:编辑帖子以包含 POJO BillWithProducts.java(它在我的代码中但不在帖子中)。

最佳答案

好的,为了测试,我尝试在我的产品表中添加数量列,代码运行顺利。
问题是我在 BillWithProducts.java 中的 productsMinimal 列表包含数量字段为 0 的所有产品。
好像查询没有考虑分组依据和计数。
(我删除了这个数量栏,它只是为了测试而添加的)

然后,我尝试使用类似的场景查询我的数据库,但使用 getDatabase().productDao().getAllProductsMinimal(bill_id) 调用“超出”此账单/产品关系。

Dao ProductDao.java :

@Dao
public abstract class ProductDao{
@Query("select * from product")
public abstract List<Product> getAllProducts();

@Query("select id, bill_id, price, name, " +
"count(*) as quantity, sum(price) as sumPrices " +
"from product " +
"group by name " +
"where bill_id = :bill_id")
public abstract List<ProductMinimal> getAllProductsMinimal(int bill_id);
}

在这里我可以得到我想要的结果!
所以我最终想到的是 @Relation 注释不允许任何条件。

最后,我保留了原始的@Relation,得到了我所有的产品,具有名称冗余,并从原始列表中用 java 中的名称创建了我自己的数量和总和列表。
由于账单上不会有大量产品,而且产品列表仅显示在更多详细信息屏幕上,因此在单击此更多详细信息按钮时构建我的列表不会延迟所需数据的显示。

关于android - Room 中的 Sum 和 Count - 别名没有这样的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50841252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com