Here is my simple function:
public int countCats(String tableName) {
int catCount = 0;
Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM " + MySQLiteHelper.TABLE_CAT, null);
if (cursor != null) {
catCount = cursor.getColumnIndex("COUNT");
}
return catCount;
}
There are 11 rows int this table. But this function returns -1. How is appropriate way to handle this?
EDIT:
I have updated to this:
public int countCats(String tableName) {
int catCount = 0;
Cursor cursor = database.rawQuery("SELECT COUNT("+ MySQLiteHelper.COLUMN_CAT+ ") FROM " + MySQLiteHelper.TABLE_CAT, null);
if (cursor.moveToFirst()) {
catCount = cursor.getInt(0);
}
cursor.close();
return catCount;
}
Now I get 0;
getColumnIndexOrThrow(String)to see what happens ("if you expect the column to exist usegetColumnIndexOrThrow(String)instead, which will make the error more clear.")