'How to get auto increment value in prepared command in postgresql?
I create table with this sql command:
create table recorddata
( id serial,
name char(20),
age int);
just like auto increment in mysql, I want id is auto increment in postgresql.
then, insert data in a transaction.sql command: begin, insert..., commit.
I use libpg.a to do this:
first, PQexec(conn, "begin);
then, PQprepare(conn, "insert", "insert into recorddata (name, age) values ($1, $2)", 2, NULL)
and, PQexecPrepared to insert actual data
final, PQexec(conn, "end")
But, how can i get id value for each time execute function PQexecPrepared ? I tried using SELECT nextval after execute PQexecPrepared, but i can not query in a insert transaction; and i also tried using sql command:insert into recorddata (name, age) values (&1, $2) return id, but it not work.
Solution 1:[1]
It should work with: insert into recorddata (name, age) values (&1, $2) return id Just like you do some Query SQL statement which returns a result set.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SeanH |
