Search code examples
androidandroid-sqlite

How to get last inserted row in SQlite android


I want to get the last inserted row's primary key value which is set automatically(AUTO INCREMENT). I searched over the S/O and found answers for last row_id. But I need the last inserted row's values. Please help me.

Thanks


Solution

  • Try this code

    String selectQuery = "SELECT  * FROM " + "sqlite_sequence";
    Cursor cursor = db.rawQuery(selectQuery, null);
    cursor.moveToLast();
    

    or

    SELECT * 
    FROM    TABLE
    WHERE   ID = (SELECT MAX(ID)  FROM TABLE);
    

    or

    SELECT * FROM table ORDER BY column DESC LIMIT 1;