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;
}    
Jacob
  • 77,566
  • 24
  • 149
  • 228
David
  • 2,691
  • 7
  • 38
  • 50
  • Unrelated to question: You can't iterate a cursor by calling "moveToFirst" indefinitely. You should call it once and then use while(cursor.moveToNext()) { ... – Ankhwatcher Oct 26 '17 at 16:02

8 Answers8

107

Cursor.getCount()

michaelg
  • 2,692
  • 1
  • 17
  • 16
8
cursor.moveToNext();
cursor.getCount();

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

Pramod Setlur
  • 811
  • 1
  • 15
  • 27
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 :)

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • 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? – AJW Sep 13 '18 at 23:35
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();
LK Yeung
  • 3,462
  • 5
  • 26
  • 39
0

In DatabaseUtils

public static long queryNumEntries(SQLiteDatabase db, String table)
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
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);
}
Rohit
  • 13
  • 1
  • 3
0
val query = "SELECT * FROM $TABLE_NAME ;"
val result = db.rawQuery(query,null)
Toast.makeText(ctx,result.count,Toast.LENGTH_SHORT).show()
shim
  • 9,289
  • 12
  • 69
  • 108
Stan
  • 1
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.

Gopal
  • 1,719
  • 12
  • 13