Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Examples of using PQexecParams

1,341 views
Skip to first unread message

Daniel

unread,
Dec 4, 2009, 1:47:47 PM12/4/09
to
I was looking for examples of using PQexecParams but all I found was
this: http://sepp.oetiker.ch/postgresql-8.2.3-ds/libpq-example.html

/* Convert integer value "2" to network byte order */
binaryIntVal = htonl((uint32_t) 2);
/* Set up parameter arrays for PQexecParams */
paramValues[0] = (char *) &binaryIntVal;
paramLengths[0] = sizeof(binaryIntVal);
paramFormats[0] = 1; /* binary */
...
seems a bit convoluted.

In particular I was looking for an example of setting up a string
parameter for PQexecParams, assuming that is possible.

John R Pierce

unread,
Dec 4, 2009, 2:50:03 PM12/4/09
to

strings don't need to be passed in binary, so its a lot simpler.

see example 30-3 on
http://www.postgresql.org/docs/current/static/libpq-example.html

const char *paramValues[1];

/* Here is our out-of-line parameter value */
paramValues[0] = "joe's place";

res = PQexecParams(conn,
"SELECT * FROM test1 WHERE t = $1",
1, /* one param */
NULL, /* let the backend deduce param type */
paramValues,
NULL, /* don't need param lengths since text */
NULL, /* default to all text params */
0); /* ask for text results */

--
Sent via pgsql-general mailing list (pgsql-...@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Merlin Moncure

unread,
Dec 4, 2009, 4:05:13 PM12/4/09
to
On Fri, Dec 4, 2009 at 1:47 PM, Daniel <danw...@gmail.com> wrote:
> I was looking for examples of using PQexecParams but all I found was
> this: http://sepp.oetiker.ch/postgresql-8.2.3-ds/libpq-example.html
>
> /* Convert integer value "2" to network byte order */
>    binaryIntVal = htonl((uint32_t) 2);
> /* Set up parameter arrays for PQexecParams */
>    paramValues[0] = (char *) &binaryIntVal;
>    paramLengths[0] = sizeof(binaryIntVal);
>    paramFormats[0] = 1;        /* binary */

check out libpqtypes (http://libpqtypes.esilo.com). It does all the
binary stuff for you (which you don't need to to with PQexecParams).

with libpqtypes, you can do stuff like:
res = PQexecf(conn, "insert into foo values (%int4, %text)", 2, "test");

note: libpqtypes requires a patched libpq for client versions < 8.4

merlin

Daniel

unread,
Dec 5, 2009, 1:45:16 PM12/5/09
to
> check out libpqtypes (http://libpqtypes.esilo.com).  It does all the
> binary stuff for you (which you don't need to to with PQexecParams).
>

thanks, but how do you install it on Gentoo, there is no ebuild.

0 new messages