如何在不使用SELECT查询的情况下在Room持久性数据库中获得具有布尔值true(或1)的行数计数? [英] How can i get count of number of rows that have boolean value true(or 1) in Room persistent database without using SELECT query?
问题描述
我正在我的项目中使用Room持久性数据库.我有一个表,其中有一个布尔值列,如0或1,现在我要对所有值为true(或1)的布尔值进行计数.
I am working with Room persistent database in my project. I have a table in which there is a column for Boolean values as in 0 or 1, now i want the count of all Boolean values whose value is true (or 1).
我知道我可以使用select查询通过使用where子句获取所有选定行的计数来实现这一点!
I know that i can achieve this using select query by getting the count of all selected rows using where clause!
但是我不想为此使用带位置子句的Select查询,因为它将加载所有行,然后我将得到计数,但是我希望该计数不加载任何行!请提出其他简单的解决方案!谢谢!
But i don't want to use Select query with where clause for this because it will load all the rows and then i will get the count, but i want the count without loading any rows! Suggest other simple solutions please! Thank you!
推荐答案
最后,我得到了完美的解决方案!只需在DAO类中添加此方法,如下所示:
Finally I got the perfect solution! Just add this method in the DAO class as follows:
@Query("SELECT COUNT(is_checked) FROM table WHERE is_checked = 1")
int getNumberOfRows();
感谢Florina Muntenescu在 https://medium.com/@florina.muntenescu
All thanks to Florina Muntenescu at https://medium.com/@florina.muntenescu
这篇关于如何在不使用SELECT查询的情况下在Room持久性数据库中获得具有布尔值true(或1)的行数计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!