Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

I know that i can achieve this using select query by getting the count of all selected rows using where clause!

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!

like image 750
Priyanka Alachiya Avatar asked Dec 18 '17 07:12

Priyanka Alachiya


People also ask

How do I count the number of rows returned by a query in MySQL?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

What is the most performance way to get the total number of records from a table?

The best way to get the record count is to use the sys. dm_db_partition_stats or sys.

How do I count rows in SQLite?

In SQLite the Count(*) function will return total number of rows available in a table, including the rows which contain NULL values. The Count(*) will not take any parameters other than the asterisk symbol (*).

How can we check data is inserted or not in room database?

In Android Studio 4.1 and higher, there's a feature called Database Inspector that lets you see the stored data in the database! Go to View > Tools > App Inspection, select your app from the list, and double-click the notes database.


1 Answers

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

like image 89
Priyanka Alachiya Avatar answered Sep 19 '22 02:09

Priyanka Alachiya