How can i get count of number of rows that have boolean value true(or 1) in Room persistent database without using SELECT query?

只愿长相守 提交于 2019-12-03 11:20:22

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();

All thanks to Florina Muntenescu at https://medium.com/@florina.muntenescu

Using aggregate function sum may help you:

select
    sum(
        case
            when t.VALUE = 1 then
                1
            else
                0
        end
    )
from
    table t

Please note, SQLite engine will read entire table, if no indexed by aggregating field. It's no problem if there only few records in the table, otherwise it's better to consider using from keyword of indexed fields.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!