1

I have the following code where Im trying trying to grab all rows that matches the id.

public ChartData getCompliedChartCounts(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();


        Cursor cursor = db.query(TABLE_COMPLIANCE_CHART_COUNTS, new String[]{KEY_ID,
                        KEY_FILTER_ID, KEY_YEAR, KEY_COUNTRY_ID, KEY_DOMAIN_ID, KEY_COMPLIED_COUNT, KEY_NOT_COMPLIED_COUNT, KEY_DELAYED_COUNT, KEY_INPROGRESS_COUNT}, KEY_COUNTRY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        ChartData country = new ChartData(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5), cursor.getInt(6), cursor.getInt(7), cursor.getInt(8));
        // return contact
        return country;
    }

When Im invoking it like getCompliedChartCounts(3) , I want it to grab all rows that contains the id 3.

But problem is that, it grabs only the first row that matches the id 3 whereas the remaining rows are not being fetched. How can I sort this out?

  • How do you know it's grabbing only one row? You're not iterating over the `Cursor`. Are you checking its `getCount()`? – Mike M. Apr 14 '16 at 05:23
  • Have you tried looping cursor like `while (cursor.moveToNext())` ? – Shree Krishna Apr 14 '16 at 05:24
  • I tried logging and it only logs only the first element that matches the id –  Apr 14 '16 at 05:26
  • I tried using moveToNext() but still the same problem is persisting –  Apr 14 '16 at 05:27
  • Fallows this link i hope it will work for you http://stackoverflow.com/questions/10723770/whats-the-best-way-to-iterate-an-android-cursor – Hasnain Apr 14 '16 at 05:31
  • No i tried it yet the problem persists –  Apr 14 '16 at 05:34
  • I want to grab all rows that matches the id 3 but only the first row is getting printed –  Apr 14 '16 at 05:35

1 Answers1

4

Dear you are not iterate the cursor . you fetch only first row you should try

try {
    while (cursor.moveToNext()) {
        ...
    }
} finally {
    cursor.close();
}

And hold the all content and grab all content

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42