Re: PQfformat question and retrieving bytea data in C - Mailing list pgsql-general

From Merlin Moncure
Subject Re: PQfformat question and retrieving bytea data in C
Date
Msg-id CAHyXU0xgH178f+BN2c3eHV=NEsaYq6Mf-pxHyY+AZJrVtFUhbw@mail.gmail.com
Whole thread Raw
In response to Re: PQfformat question and retrieving bytea data in C  (Dmitriy Igrishin <dmitigr@gmail.com>)
List pgsql-general
On Wed, Aug 29, 2012 at 8:05 AM, Dmitriy Igrishin <dmitigr@gmail.com> wrote:
> Hey Jason,
>
> 2012/8/29 Jason Armstrong <ja@riverdrums.com>
>>
>> I have a question regarding the return value of PQfformat()
>>
>> I have a 'data' column in my table, type bytea (postgresql 9.1.5).
>>
>> In postgresql.conf:
>> bytea_output = 'escape'
>>
>> When I execute the query:
>> PGresult *res = PQexec(db, "SELECT data::bytea FROM data_table WHERE
>> id='xxx'")
>
> PQexec() always returns data in the text format. You should use
> PQexecParams() to obtain the data as binary.


Also see libpqtypes.  It abstracts you from the wire format and
returns data in a regular way:

int success;
PGint4 i4;
PGtext text;
PGbytea bytea;
PGpoint pt;
PGresult *res = PQexec(conn, "SELECT i,t,b,p FROM tbl");

/* Get some field values from the result (order doesn't matter) */
success = PQgetf(res,
     0,            /* get field values from tuple 0 */
     "%int4 #text %bytea %point",
                   /* type format specifiers (get text by name '#') */
     0,   &i4,     /* get an int4 from field num 0 */
     "t", &text,   /* get a text from field name "t" */
     2,   &bytea,  /* get a bytea from field num 2 */
     3,   &pt);    /* get a point from field num 3 */

/* Output an error message using PQgeterror(3). */
if(!success)
     fprintf(stderr, "*ERROR: %s\n", PQgeterror());

/* Output the values, do this before PQclear() */
else
     printf("int4=%d, text=%s, bytea=%d bytes, point=(%f,%f)\n",
          i4, text, bytea.len, pt.x, pt.y);

PQclear(res);

merlin


pgsql-general by date:

Previous
From: tamanna madaan
Date:
Subject: calling a C function from pgsql function
Next
From: Chris Angelico
Date:
Subject: Re: PQfformat question and retrieving bytea data in C