I have a table with autoid for the id field. After inserting a row with anorm I'd like to retrieve the generated id. Any idea?
SQL(
"""
insert into accom (id,places,lat,lon,permaname,country,defaultLanguage) values (
{places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage}
)
""").on(
'id -> id,
'places -> places,
'lat -> lat,
'lon -> lon,
'permaname -> permaname,
'country -> country,
'defaultLanguage -> defaultLanguage).executeUpdate()
}
In the latest version you need a scalar:
val newId = SQL(
"""
insert into accom (places,lat,lon,permaname,country,defaultLanguage)
values ({places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage})
""").on(
'places -> places,
'lat -> lat,
'lon -> lon,
'permaname -> permaname,
'country -> country,
'defaultLanguage -> defaultLanguage).executeInsert(scalar[Long].single)
Use executeInsert
instead of executeUpdate
, and the return value is the id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With