mysql - raw query not working android -


i inserting data table , getting id of inserted item raw query returning: android.database.cursorindexoutofboundsexception: index -1 requested, 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 + ") " + col_athlete_id +     " " + table_athletes, null);     int id = cursor.getint(cursor.getcolumnindex(col_athlete_id));     athlete.setathleteid(id);     log.e("", "id: " + id); 

sqlite query methods return cursor object positioned before first entry. need call movetofirst() or movetonext() before try read cursor, e.g.:

cursor.movetofirst(); 

that method returns boolean value indicating whether successful. example if results set empty, return false , know not try read value. in specific case, nature of query such should return single row, such test arguably not required. hence single line of code above should suffice.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -