Niraj Niraj - 1 month ago 9
Android Question

How to count the number of records by using a specific column in a Cursor?

I wrote an app which contains a SQLite database and having a table named "notification".

I want to get the record count by using the column named "packageName" from the afore mentioned table.

How do I do that?

code:

public long getRecordsCount() {
SQLiteDatabase db = this.getReadableDatabase();
long count = DatabaseUtils.queryNumEntries(db, CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME);
db.close();
return count;
}

Answer Source

Try This :

public long getRecordsCount() {
    SQLiteDatabase db = getReadableDatabase();
    try {
        String selectQuery =
                "SELECT COUNT(*)" +
                " FROM " + CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME +
                " WHERE " + YOUR_SPECIFIC_COLUMN + " = 'COLUMN_VALUE'";
        return DatabaseUtils.longForQuery(db, selectQuery, null);
    } finally {
        db.close();
    }
}