Search code examples
javasqlitecursor

cursor.getCount() returning wrong count using rawQuery


I have used rawQuery to fetch records from DB table. I have checked the query from log and it is executing perfectly in SQLite. But the cursor.getCount is returning wrong row count and cursor containing wrong resultset. I have used the following code:

Cursor productCursor = dataHelper.rawQuery(query_str, null);
int list_count = productCursor.getCount();
Log.d("list_count", ""+list_count);
productCursor.moveToFirst();
while(productCursor.isAfterLast() == false) {
    ......
}

There are actually 4 records but the cursor contains only 3 records. Tested in SQLite and got the correct resultset. It would be helpful if anyone can point out my fault.


Solution

  • try this..

    productCursor.moveToFirst();
    
    do {
      // your code..
    
    } while (productCursor.moveToNext());