How do I retrieve the key from an insert to a table that has an auto increment of primary key? I need to get the key of the row just inserted.
I could simply retrieve the row but I was look for another way to do this
Thanks
How do I retrieve the key from an insert to a table that has an auto increment of primary key? I need to get the key of the row just inserted.
I could simply retrieve the row but I was look for another way to do this
Thanks
Hello @Eagle442BR,
Quoting the Executing Updates documentation chapter:
After an INSERT statement, you can get the row ID of the inserted row:
try db.execute( sql: "INSERT INTO player (name, score) VALUES (?, ?)", arguments: ["Arthur", 1000]) let playerId = db.lastInsertedRowID
See also the PersistableRecord Protocol documentation chapter. This protocol can update a record object with its new auto-incremented id:
var paris = Place( id: nil, title: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8534100, longitude: 2.3488000)) try paris.insert(db) paris.id // some value
The GRDB documentation is your friend, @Eagle442BR: read it, search it, use it!
Thanks for the answer, Point well taken!