Thursday, 29 August 2013

How to return id from Insert with libpqtype

How to return id from Insert with libpqtype

I have a c script which writes incoming aircraft data to a postgres db. I
would like to return the new records ID after the INSERT but am having
trouble.
The simplified code is like this:
int new_id;
conn = PQconnectdb("dbname = flight_dev");
PQinitTypes(conn);
PGresult *res = PQexecf(conn,"INSERT INTO aircrafts (hex) VALUES (%text)",
aircraft_hex);
PQgetf(res, 0, "%int4", 0, &new_id);
The INSERT is successful however new_id is not assigned and gives the
error "row number 1 is out of range 0..-1"
Any help would be great thanks.

No comments:

Post a Comment