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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Roy's Email" <rmw256(at)hotmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: libpq - getting value of SERIAL field when INSERT-ing?
Date: 2011-08-31 00:12:48
Message-ID: 620.1314749568@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Roy's Email" <rmw256(at)hotmail(dot)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 anything in 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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message 金 今花 2011-08-31 11:52:21 pg_dump error
Previous Message Roy's Email 2011-08-31 00:08:07 libpq - getting value of SERIAL field when INSERT-ing?