Re: htonl necesssary for PQexecParams input?

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: htonl necesssary for PQexecParams input?
Дата
Msg-id 20060622165314.GA32808@winnie.fuhr.org
обсуждение исходный текст
Ответ на htonl necesssary for PQexecParams input?  (Kevin Jenkins <gameprogrammer@rakkar.org>)
Ответы Re: htonl necesssary for PQexecParams input?  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
On Thu, Jun 22, 2006 at 08:52:04AM -0700, Kevin Jenkins wrote:
> Previously I was getting a bad result when calling PQexecParams with
> binary results because PostgreSQL stores its data big endian.  So I
> had to do ntohl on the result to get it to little endian.

Clarification: PostgreSQL stores data in host byte order but returns
it in network byte order if you request binary format.

> My question is, do I also need to do htonl then, as in this scenario?
>
> outStr[0]="blah";
> outLengths[0]=htonl((int)strlen("blah"));
> formats[0]=1;
> PQexecParams(pgConn, query,1,0,outStr,outLengths,formats,1);

Only the data needs byte order conversion; if you convert lengths
then you'll probably get a database error or segmentation fault
(assuming you're on a machine where host and network byte order
differ).  I tested the above and PQexecParams failed with

  ERROR:  invalid byte sequence for encoding "SQL_ASCII": 0x00

A packet sniff of the connection showed a large amount of data being
transferred (0x04000000 bytes instead of 0x00000004 bytes), so the
length needs to be in host byte order.

--
Michael Fuhr

В списке pgsql-general по дате отправления:

Предыдущее
От: zzzeek@gmail.com
Дата:
Сообщение: Re: best way to get PKey and FKey from inside applications (permission pb)
Следующее
От: Tom Lane
Дата:
Сообщение: Re: htonl necesssary for PQexecParams input?