0

I have tried to figure this out and googled through a fair few stack overflow responses but none of them seem to address this exact problem. I am trying to return the row ID so that I can update my database.

    String query= "SELECT * FROM climb_tbl WHERE climb_name ='"+climbname+"'";
    Cursor cur = db.rawQuery(query, null);
    cur.moveToFirst();
    long id = cur.getLong(cur.getColumnIndex("_id"));
    System.out.println(id);

As far as I can tell this should return the id value I need to then run the update statement, ive yet to write. Instead I get the error.....

java.lang.IllegalStateException: get field slot from row 0 col -1 failed.
saoirse
  • 53
  • 5

1 Answers1

2
  1. Do you have _id column in your table?

  2. Probably the cursor is empty. The cur.moveToFirst() returns true or false, so you can use it in the following way:

    if (cur != null && cur.moveToFirst()) {
        long id = cur.getLong(cur.getColumnIndex("_id"));
        System.out.println(id);
    }
    
Blehi
  • 1,990
  • 1
  • 18
  • 20
  • Thankyou very much it was a problem with the cursor. – saoirse Apr 03 '12 at 16:47
  • 1
    Suggestion: Log.i("YourClassName","Your message"); is better than System.out.printline(""). Please check this link : http://stackoverflow.com/questions/2220547/why-doesnt-system-out-println-work-in-android – Blehi Apr 03 '12 at 20:41