Binoy Babu
Binoy Babu

Reputation: 17139

Counting SQLite rows

How to count the number of rows in an SQLite database table? I have a table called my_table and it has columns name info and number.

Upvotes: 3

Views: 8987

Answers (2)

kosa
kosa

Reputation: 66657

You may use rawQuery count(*) which returns number of rows in a table.

   cursor=db.rawQuery("Select count(*) from my_table;", null);

Upvotes: 10

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53687

Use SELECT COUNT(*) FROM " + DB_TABLE_PLACES query to get number of rows present in the table Example

private static final String DB_TABLE_PLACES = "my_table";
private SQLiteDatabase mDatabase;

private long fetchPlacesCount() {
    String sql = "SELECT COUNT(*) FROM " + DB_TABLE_PLACES;
    SQLiteStatement statement = mDatabase.compileStatement(sql);
    long count = statement.simpleQueryForLong();
    return count;
}

Upvotes: 4

Related Questions