How to use the ORMLite query builder to get the total records in a table

20,718

Solution 1

How to use the ORMLite query builder to get the total records in a table

ORMLite has a Dao.countOf() method that returns the total number of rows in a table:

long numRows = dao.countOf();

You can also count the number of rows in a custom query by calling the countOf() method on the Where or QueryBuilder object.

// count the number of lines in this custom query
long numRows = dao.queryBuilder().where().eq("name", "Joe Smith").countOf();

Solution 2

for package 5: you can use countOf()

From the docs:

Returns the value returned from a SELECT COUNT(*) query which is the number of rows in the table. Depending on the database and the size of the table, this could be expensive.

Share:
20,718

Related videos on Youtube

Sourabh Saldi
Author by

Sourabh Saldi

Updated on February 01, 2020

Comments

  • Sourabh Saldi
    Sourabh Saldi over 4 years

    Similar to

    select count(*) from tablename;
    

    what should be query in ORMLITE

    i tried something like

    int total = dao.queryBuilder().("select count(*)");
    
  • PoeHaH
    PoeHaH almost 12 years
    I'm not really familiar with ormlite. I just searched.What about: dao.queryBuilder().countOf();
  • PoeHaH
    PoeHaH almost 12 years
    Did it work ? if so, please help me,yourself and the community by accepting this as the right answer
  • KJEjava48
    KJEjava48 over 7 years
    countOf() will return long value not int
  • Gray
    Gray over 7 years
    Thanks @KJEjava48. Fixed.