0

Im playing with sql in android and found this issue:

Im reading data from my database:

    SQLiteDatabase db = this.getReadableDatabase();
    cursor = db.rawQuery("SELECT * FROM " + TABLE_SINGLE_APPS + " WHERE " + COLUMN_SINGLE_PACKAGE + "=?"
            , new String[]{packageName});

    SingleAppModel singleAppModel = new SingleAppModel();

and when I try to get cursor.getInt(3) over here:

    try {
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                singleAppModel.setId(cursor.getInt(0));
                singleAppModel.setAppName(cursor.getString(1));
                singleAppModel.setPackageName(cursor.getString(2));
                singleAppModel.setState(cursor.getInt(3) == 1);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    cursor.close();
    db.close();

It returns error:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0`

Weird is that if I delete line where Im calling cursor.getInt(3), it works. Colum with that value exists and Im sure its of type INTEGER.

Also the value of cursor.getColumnCount() is 4 and value of cursor.getCount() is 1 ... so there are definetly some data...

Any advice?

Thankyou.

2
  • I think your cursor is not what you are expecting. try putting debug point on the if condition and check what data is coming Commented Oct 24, 2015 at 21:44
  • What do you mean by debug point? Im kind of beginner programer so if you could be more accurate I would really appreciate it. Commented Oct 24, 2015 at 22:37

1 Answer 1

1

Rather than using * I suggest you to use,

 cursor = db.rawQuery("SELECT COL0, COL1, COL2, COL3 FROM " + TABLE_SINGLE_APPS + " WHERE " + COLUMN_SINGLE_PACKAGE + "=?"
            , new String[]{packageName});

Use the appropriate values for COL0, COL1, COL2, COL3 according to your table. That way you make sure that the order of the columns fetched in the query.

But according to this,

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0`

I need more information to confirm, can you add your table schema, according to the issue there is no 3rd column in the database raw.

Edit:

This can be a small programmer mistake,

Look out for the onCreate and onUpgrade methods in SQLiteOpenHelper implementation

Sign up to request clarification or add additional context in comments.

5 Comments

String CREATE_SINGLE_APPS_TABLE = "CREATE TABLE " + TABLE_SINGLE_APPS + "(" + COLUMN_SINGLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_SINGLE_NAME + " TEXT," + COLUMN_SINGLE_PACKAGE + " TEXT," + COLUMN_SINGLE_STATE + " INTEGER" + ")";
There is a huge possibility that your android application is using earlier database from your previous version, There is a specific way of handling this, you can uninstall the application and install it again, that way it will recreate the database fresh.
No I have tried this many times.. I have even deleted database manualy and recreated it..
Did you try taking this cursor.getInt(3) == 1 our of the method argument and place it separately, int x = cursor.getInt(3); and boolean t = x==1; singleAppModel.setState(t); This will give more insight to the issue.
int value = cursor.getInt(3); singleAppModel.setState(value == 1); tried it like this but still the same error :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.