2

I need to get last inserted row's id in ActiveAndroid database table and I can't find any information about that in documentation. Is there proper method to do this?

elhm
  • 145
  • 1
  • 10

3 Answers3

1

Get last record of the table and then pick auto-generated id something like this...

Model model = new Select().from(Model.class).orderBy("id DESC").executeSingle();
long id = (model != null) ? model.getId() : 0;

Here I've checked for Null because if table is empty it may be null.

Or hit this as following...

long id = new Select().from(Model.class).orderBy("id DESC").executeSingle().getId();
User
  • 4,023
  • 4
  • 37
  • 63
0

if it is an sqlite database and you can query this table you can use a sql function in your query:

last_insert_rowid() The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function. ...

documentation taken from: http://www.sqlite.org/lang_corefunc.html

klas2iop
  • 128
  • 2
  • 5
0
int insert_id;

User user = new User();
user.name = "Louis";
user.password = 12345;
user.save();

insert_id = user.getId().intValue();

Hope this can help you!

udondan
  • 57,263
  • 20
  • 190
  • 175
Louis Hong
  • 41
  • 1
  • 6