You missed out
mcursor.moveToFirst();
public void count(){
SQLiteDatabase db = table.getWritableDatabase();
String count = "SELECT count(*) FROM table";
Cursor mcursor = db.rawQuery(count, null);
mcursor.moveToFirst();
int icount = mcursor.getInt(0);
System.out.println("NUMBER IN DB: " + icount);
}
But you should use better methods for this task, like the inbuilt helpers of DatabaseUtils
such as
public long count() {
return DatabaseUtils.queryNumEntries(db,'tablename');
}
You might find helpful to use DatabaseUtils.longForQuery()
to get the first column long value, when you have where query for the total count. It's simpler and you do not need that bunch of work with the Cursor.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…