Обсуждение: int values from PQExecParams in binary result mode

Поиск
Список
Период
Сортировка

int values from PQExecParams in binary result mode

От
Ben
Дата:
I'm making a query to return an int and a bytea as two columns in my
query, so I set up my PQExecParams call like so:

res = PQexecParams(conn,q,0,0,0,0,0,1)

I can view the binary data just fine, but not the int. How do I do that?

Re: int values from PQExecParams in binary result mode

От
Michael Fuhr
Дата:
On Mon, Oct 03, 2005 at 12:37:30PM -0700, Ben wrote:
> I'm making a query to return an int and a bytea as two columns in my
> query, so I set up my PQExecParams call like so:
>
> res = PQexecParams(conn,q,0,0,0,0,0,1)
>
> I can view the binary data just fine, but not the int. How do I do that?

The pointer that PQgetvalue() returns should point to the integer
value in network byte order.  Unless somebody suggests a better
way, you could cast PQgetvalue()'s return value to int * and
dereference it, or otherwise copy the data into an integer variable.
Use ntohl() to convert the value from network byte order to host
byte order (ntohl() and htonl() are null operations on big-endian
architectures, but it's a good idea to use them in any case for
portability and so you don't have to worry about such hardware
details).

The following example works for me (error and NULL checking omitted):

res = PQexecParams(conn, "SELECT 123456::integer", 0, NULL, NULL, NULL, NULL, 1);
ival = ntohl(*(int *)PQgetvalue(res, 0, 0));

--
Michael Fuhr

Re: int values from PQExecParams in binary result mode

От
Ben
Дата:
Perfect, thanks!

On Oct 3, 2005, at 4:29 PM, Michael Fuhr wrote:

> On Mon, Oct 03, 2005 at 12:37:30PM -0700, Ben wrote:
>
>> I'm making a query to return an int and a bytea as two columns in my
>> query, so I set up my PQExecParams call like so:
>>
>> res = PQexecParams(conn,q,0,0,0,0,0,1)
>>
>> I can view the binary data just fine, but not the int. How do I do
>> that?
>>
>
> The pointer that PQgetvalue() returns should point to the integer
> value in network byte order.  Unless somebody suggests a better
> way, you could cast PQgetvalue()'s return value to int * and
> dereference it, or otherwise copy the data into an integer variable.
> Use ntohl() to convert the value from network byte order to host
> byte order (ntohl() and htonl() are null operations on big-endian
> architectures, but it's a good idea to use them in any case for
> portability and so you don't have to worry about such hardware
> details).
>
> The following example works for me (error and NULL checking omitted):
>
> res = PQexecParams(conn, "SELECT 123456::integer", 0, NULL, NULL,
> NULL, NULL, 1);
> ival = ntohl(*(int *)PQgetvalue(res, 0, 0));
>
> --
> Michael Fuhr
>