Niraj Niraj - 6 months ago 40
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?


public long getRecordsCount() {
SQLiteDatabase db = this.getReadableDatabase();
long count = DatabaseUtils.queryNumEntries(db, CNotificationDataBaseTableDeclarations.NotificationsTable.TABLE_NAME);
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 {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download