Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql PQgetvalue: array return

I've a table created like:

CREATE TABLE tbl_test
(
  id        bigserial    PRIMARY KEY,
  interest  int          ARRAY[2]
);

I got PGresult* res using PQexec(conn, "SELECT * FROM tbl_test");

Now, how can I get int[] from PQgetvalue(res, 0, 1).
I don't want to depend on structs defined in array.h as they might change.

I could not find any API in Postgresql docs which can do things.
Please advise.

Regards,
Mayank

like image 874
Mayank Avatar asked May 02 '11 20:05

Mayank


1 Answers

PQgetvalue() returns the string representation of the field value unless you specify a binary cursor. In either case (string or binary cursor) you'll need to supply the code to manipulate the result into the form you want.

You might find some ideas in the PostgreSQL source code.

  • src/backend/utils/adt/arrayfuncs.c
  • src/include/utils/array.h.

And there's some code in contrib/intarray.

At http://doxygen.postgresql.org/, click "Files", then search for "array".

At the string level, which is what PQgetvalue() returns, it's probably easy to Google up some code that extracts integers from an comma-delimited string.

like image 177
Mike Sherrill 'Cat Recall' Avatar answered Oct 22 '22 14:10

Mike Sherrill 'Cat Recall'