Home > DeveloperSection > Forums > CursorIndexOutOfBoundsException for table and records existing
Mark Devid
Mark Devid

Total Post:102

Posted on    May-30-2013 12:15 AM

 Android Android 

 1 Reply(s)
 1202  View(s)
Rate this:
Hi Expert!

I am querying the database with the following query:

final Cursor subject_cursor = db.rawQuery("SELECT * FROM " + DB.Table.SUBJECT + " WHERE uniqueSUB = '" + cs_cursor.getString(1) + "'", null);
when i log the cursor count it gives me 1 as the count, but when i try to access the data using subject_cursor.getString(0) it gives me an error saying:

    05-30 03:37:10.921: W/System.err(5408): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 I have checked the database for the records manually and all the required data is present. (Also if i just do a following query:

    "SELECT * FROM " + DB.Table.SUBJECT"
for the same table and give the get the count for the cursor it gives me the existing number of records which is correct).

Please Do Help...

Thanks in advance. 


Total Post:604

Posted on    May-30-2013 5:54 AM

Hi Mark,

Try this, maybe you made a mistake

final Cursor subject_cursor = db.rawQuery("SELECT * FROM " + DB.Table.SUBJECT + " WHERE uniqueSUB = '" + cs_cursor.getString(0) + "'", null);

Because you have only 1 row in your table, and your are trying to fetch 2nd row using cs_cursor.getString(1)

Or try this code,

public void getAllEvents() {
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_EVENTS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            // Adding contact to list
            MainActivity.Id.add(cursor.getString(0)); //it will store id to my array list "Id"
        } while (cursor.moveToNext());

I hope this code might be resolve your problem. 

Don't want to miss updates? Please click the below button!

Follow MindStick