Re: htonl necesssary for PQexecParams input?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Kevin Jenkins <gameprogrammer(at)rakkar(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: htonl necesssary for PQexecParams input?
Date: 2006-06-22 16:53:14
Message-ID: 20060622165314.GA32808@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dylan Hansen 2006-06-22 17:18:50 auto-vacuum & Negative "anl" Values
Previous Message Tom Lane 2006-06-22 15:56:53 Re: VACUUM hanging on idle system