Re: PQoidValue - isn't it for...?

From: Magnus Hagander <magnus(at)hagander(dot)net>
To: Jim Michaels <jmichae3(at)yahoo(dot)com>
Cc: pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: PQoidValue - isn't it for...?
Date: 2009-08-12 08:17:55
Message-ID: 9837222c0908120117s334e45a0p2cdd969fc258d51b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Aug 12, 2009 at 10:11, Jim Michaels<jmichae3(at)yahoo(dot)com> wrote:
> I am struggling to learn libpq.
>
> for some reason, I could not get an INSERT to produce an Oid.  actually,

By default, tables are created without Oids.

> what I am looking for, is to get the ID of the last record inserted or to
> verify that I inserted a record successfully.  I think you use
> PQresultStatus() for that.(?)

If you want to get the ID, use something like:
INSERT INTO ... VALUES (...) RETURNING id

Assuming "id" is the name of your serial column. It will then return a
regular resultset that you can access with PQgetvalue().

But, if you just want to know if the insert succeeded or not, the
check of PQresultStatus() is enough. If that one returns
PGRES_COMMAND_OK, it means the INSERT was ok. There is no need for
your application to do any further verification.

> Isn't PQoidValue() for getting the last INSERT id?  or am I misunderstanding
> it?

It will get you the last oid, *if* the table has Oids, which user
tables by default don't have. And in most cases, they shouldn't have -
using a SERIAL or BIGSERIAL column is in most cases much better.

--
Magnus Hagander
Me: http://www.hagander.net/
Work: http://www.redpill-linpro.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2009-08-12 08:18:24 Re: ERROR: XLogFlush: request AF/5703EDC8 is not satisfied --- flushed only to AF/50F15ABC
Previous Message Tommy Gildseth 2009-08-12 08:12:02 Re: Best way to "mask" password in DBLINK