Here’s a little function from my Database Adapter class in the app I’m working on. It returns the number of rows in the “Places” table from my database.

It does this using a SQLiteStatement object, much simpler and cleaner than getting a Cursor using SQLiteDatabase.rawQuery().

A SQLiteStatement can be used for database queries that return “1×1 result sets”, that is one row with one column – a single value.

private static final String DB_TABLE_PLACES = "Places";
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;
}