How To Get The Total Count Of Records From Sqlite
Solution 1:
Try c.getInt(0);
or
c = getInstance(_context).db.rawQuery("SELECT COUNT(word) as WORD_COL FROM "
+ WORD_TABLE, null);
also put a c.moveToFirst();
right after the rawQuery line for good measure.
Solution 2:
Assuming WORD_COL
is word
? You have not requested a column named word
. You have a column COUNT(word)
, which is a completely different matter; thus when you try to fetch data for word
, getColumnIndex
fails to find it, giving you a nonsense index of -1
, which then causes the getInt
to fail since nobody counts from -1
up. :)
You can rename the column to something nicer, too: SELECT COUNT(word) AS word_count FROM...
, then request that name. Another option is, you know this query returns only one column, there is no need to name it; just use its index directly.
Solution 3:
After the rawQuery
, try
c.moveToFirst();
return c.GetInt(0);
Solution 4:
try out this:
Cursor c = _sqlDatabase.rawQuery("Select * from table", null);
System.out.println("Count:"+c.getCount());
regards, Nital Shah
Solution 5:
You may have this CursorIndexOutOfBoundsException
because the cursor doesnt point to the first result.
Try this:
try
{
c = getInstance(_context).db.rawQuery("SELECT COUNT(word) FROM "
+ WORD_TABLE, null);
c.moveToFirst();
return c.getInt(c.getColumnIndex(WORD_COL));
} catch (Exception e)
Post a Comment for "How To Get The Total Count Of Records From Sqlite"