Thread: libpq - getting value of SERIAL field when INSERT-ing?

libpq - getting value of SERIAL field when INSERT-ing?

From
Roy's Email
Date:
Using libpq, how do I get the return value from an INSERT statement?
 
CREATE TABLE foo ( pk SERIAL , val text, PRIMARY KEY (pk) );
INSERT INTO foo VALUES ( DEFAULT, 'stuff' ) RETURNING (pk);
 
How do I get the value assigned to 'pk'?  I assume it's in the 'PGresult' returned from 'PQexec()', but I can't find anything in the documentation for 'PGresult' that appears to discuss this particular use.

Regards,

- Roy

=*=*=
Always do right.
This will gratify some people and astonish the rest. - Mark Twain

The truth is rarely pure, and never simple. - Oscar Wilde
 

Re: libpq - getting value of SERIAL field when INSERT-ing?

From
Tom Lane
Date:
"Roy's Email" <rmw256@hotmail.com> writes:
> Using libpq, how do I get the return value from an INSERT statement?

> CREATE TABLE foo ( pk SERIAL , val text, PRIMARY KEY (pk) );
> INSERT INTO foo VALUES ( DEFAULT, 'stuff' ) RETURNING (pk);

> How do I get the value assigned to 'pk'?  I assume it's in the 'PGresult' returned from 'PQexec()', but I can't find
anythingin the documentation for 'PGresult' that appears to discuss this particular use. 

The PGresult is exactly the same as if you'd done "SELECT pk FROM ..."

            regards, tom lane