Search code examples
androidandroid-sqliteandroid-cursor

Android Cursor not looping through rows


I have this table in SQLite enter image description here and then I have this code with which I want to get all the row values

SQLiteDatabase db = dbModel.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT cig_for_this_day FROM program WHERE stage = 2", null);
    if (c.moveToFirst()) {
        int rows = c.getCount();
        while (c.isAfterLast() == false) {
            int j = c.getInt(0);
            c.moveToNext();
        }
    }
    c.close();
    db.close();

it gets the count all right, but every time it loops it returns 12, why is that happening, I want it to move all the way to the end of the column


Solution

  • Change the loop like this:

    ...
    if (c.moveToFirst()) {
        int rows = c.getCount();
        do {
            int j = c.getInt(0);
            // Do something with j
        } while(c.moveToNext());
    }
    ...