Sandip Armal Patil
Sandip Armal Patil

Reputation: 5895

How to get _id of last record from sqlite?

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;
}

Upvotes: 1

Views: 1993

Answers (1)

Nixit Patel
Nixit Patel

Reputation: 4445

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.

Upvotes: 4

Related Questions