Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 938
Mostafa Imran Avatar asked Dec 01 '22 17:12

Mostafa Imran


2 Answers

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;
like image 190
sasikumar Avatar answered Dec 04 '22 14:12

sasikumar


try this :

select * from <TABLE> where row_id = (select max(row_id) from <TABLE>);

or

SELECT * FROM tablename ORDER BY row_id DESC LIMIT 1;
like image 34
No Name Avatar answered Dec 04 '22 14:12

No Name