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;
}
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.
If the moveToNext() is not called, the cursorIndexOutOfBoundException may arise.
Cursor.getCount()
You can use this as a method or use this if your database uses auto increment
In
DatabaseUtils
This would be more efficient because work for all versions:
or
or if you want to get number of rows which specific selection then you should go with (added in API 11)
Thanks :)