Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get saved id in SugarORM?

SugarORM, in my opinion, is the easiest SQLite library to use and proven to be extremely helpful for junior Android developer like me. SugarORM automatically creates the table and add an AUTO_INCREMENT id column for every java class extending SugarRecord.

Inserting a new row can be as easy as someJavaObject.save(). But how can I get the inserted id once that row is inserted? In PHP I can do something like $id = mysql_insert_id(); after insertion.

I understand that I can just get the id of last row in the table. But the insertion of new row can somehow be very unpredictable. Let's say I have an activity and a service inserting a new row at the same time, I want to avoid getting the wrong id.

Thanks in advance for any help.

like image 468
Hendra Anggrian Avatar asked Jan 08 '23 14:01

Hendra Anggrian


1 Answers

The save() method has to return it. Like:

 long id = someJavaObject.save() 
like image 136
Stan Avatar answered Jan 14 '23 16:01

Stan