51

How do I get the row count of a query in Android using SQLite? It seems my following method does not work.

public int getFragmentCountByMixId(int mixId) {
    int count = 0;
    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

    Cursor cursor = db.rawQuery(
        "select count(*) from downloadedFragement where mixId=?",
        new String[]{String.valueOf(mixId)});
    while(cursor.moveToFirst()){
        count = cursor.getInt(0);
    }
    return count;
}    
1
  • Unrelated to question: You can't iterate a cursor by calling "moveToFirst" indefinitely. You should call it once and then use while(cursor.moveToNext()) { ... Commented Oct 26, 2017 at 16:02

8 Answers 8

107

Cursor.getCount()

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know how this would work with your current implementation but if you simply change count(*) to *, this should work.
I think best solution return (int) DatabaseUtils.longForQuery(mDB, "SELECT COUNT(*) FROM table_name", null);
This would return 0 for a cursor that has 1 row.
8
cursor.moveToNext();
cursor.getCount();

If the moveToNext() is not called, the cursorIndexOutOfBoundException may arise.

Comments

6

This would be more efficient because work for all versions:

int numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM table_name", null);

or

int numRows = DatabaseUtils.queryNumEntries(db, "table_name");

or if you want to get number of rows which specific selection then you should go with (added in API 11)

public static long queryNumEntries (SQLiteDatabase db, String table, String selection)

Thanks :)

1 Comment

In you first example you use longForQuery(). I thought that method only returns the value in the first column of the first row. So how would that show the total number of rows in the database? Is it because the query SELECT COUNT(*) is run on the full database?
1

use String instead of int

String strCount = "";
int count = 0;
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

Cursor cursor = db.rawQuery(
    "select count(*) from downloadedFragement where mixId=?",
    new String[]{String.valueOf(mixId)});

while(cursor.moveToFirst()){
    strCount = cursor.getString(cursor.getColumnIndex("COUNT(*)"));
}
count = Integer.valueOf(strCount).intValue();

Comments

0

Query for _ID column in table and then call getCount on cursor. Here is a link I am doing in one of my project. Look at line number 110.

Comments

0

In DatabaseUtils

public static long queryNumEntries(SQLiteDatabase db, String table)

2 Comments

What if it's an in-memory database, with no database name?
Is "DatabaseUtils.queryNumEntries" still a good choice or has it been deprecated?
0
 public long getRecords() {
    return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM contacts", null);
}

You can use this as a method or use this if your database uses auto increment

 public long getRecords() {
    return DatabaseUtils.longForQuery(db, "SELECT seq FROM sqlite_sequence", null);
}

Comments

0
val query = "SELECT * FROM $TABLE_NAME ;"
val result = db.rawQuery(query,null)
Toast.makeText(ctx,result.count,Toast.LENGTH_SHORT).show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.