dimanche 14 juin 2015

raw query not working android

I am inserting data into a table and then getting the id of the inserted item but my raw query is returning: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COL_FIRST_NAME, athlete.getFirstName());
    values.put(COL_LAST_NAME, athlete.getLastName());
    values.put(COL_AGE, athlete.getAge());
    values.put(COL_GRADE, athlete.getGrade());

    db.insert(TABLE_ATHLETES, null, values);

    Cursor cursor = db.rawQuery("SELECT MAX(" + COL_ATHLETE_ID + ") AS " + COL_ATHLETE_ID +
    " FROM " + TABLE_ATHLETES, null);
    int id = cursor.getInt(cursor.getColumnIndex(COL_ATHLETE_ID));
    athlete.setAthleteID(id);
    Log.e("", "ID: " + id);

Aucun commentaire:

Enregistrer un commentaire