0

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)

Anonymous
  • 4,617
  • 9
  • 48
  • 61

1 Answers1

2

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 ;)

Kuchi
  • 4,204
  • 3
  • 29
  • 37
  • I dont know how to formulate this with soci. On this page: http://soci.sourceforge.net/doc/3.2/exchange.html are some examples but without using "returning" – Anonymous May 13 '13 at 15:43
  • Sorry seems like `RETURNING` only works with PostgreSQL since it's a extension.. http://www.postgresql.org/docs/9.3/static/sql-insert.html – Kuchi May 13 '13 at 15:58
  • 3
    luckily im using postgres and "[...] returning id", soci::into(id); works like a charm, thx! – Anonymous May 13 '13 at 17:17