Вопрос

Im using soci with C++ to access my database. Is it possible to modify the following expression in a way to get the new primary key that was given to the row which is added by that expression?

 *dbSession << "insert into myTable(myRow) values (:myVal)", soci::use(myVal);

e.g.

 long newID = *dbSession << "insert into myTable(myRow) values (:myVal)", soci::use(myVal);

So that I can continue my work by using newID? id is in this case the primary key (bigserial)

Это было полезно?

Решение

In SQL you can use RETURNING to get the generated ID.
Like: INSERT INTO tbloCustomer (Name) VALUES ('Goofy') RETURNING ID;
(If your Primary Key is called ID ;)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top