1

I have two table "TABLE_EXAM" and "TABLE_QUESTION". I need to store the question in table with exam id. My question is that how to gel last id of exam table?
Thanks in advance.. Edited Question
Here is my sample code.

public int getLastExamId() { int id2=0; try { SQLiteDatabase db=this.getWritableDatabase(); Cursor cursor=db.rawQuery("SELECT * from SQLITE_SEQUENCE", null); if(cursor.moveToLast()) { id2=cursor.getInt(0); } } catch(Exception e) { e.printStackTrace(); } return id2; } 
8
  • 2
    SELECT MAX(_ID) from TABLE_EXAM. Commented Mar 26, 2012 at 13:28
  • 1
    last record means last inserted record? Commented Mar 26, 2012 at 13:31
  • 1
    you can store id in public var.. Commented Mar 26, 2012 at 13:33
  • @imrankhan:yes last inserted record.... Commented Mar 26, 2012 at 13:39
  • 1
    you are using AUTOINCREMENT for row id? Commented Mar 26, 2012 at 13:48

1 Answer 1

4

if you are inserting data using

db.insert(TABEL_NAME, null, values); 

then the return value from this function is the id of the row.

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

2 Comments

According to developer.android.com/reference/android/database/sqlite/… it seems to be correct, but I think it is returning the number of rows affected by the operation because I am always getting 1 when inserting a new record.
actually it works perfect in my case... I have checked this so, many times for conformation... using debugger and logs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.