Libpq binary mode SELECT ... WHERE ID IN ($1) Question

From: Matt Sanchez <matt-sanchez(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Libpq binary mode SELECT ... WHERE ID IN ($1) Question
Date: 2009-11-13 06:34:14
Message-ID: 1258094054.19472.28.camel@vostro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

Suppose I have a table:
create table foo ( id int4, name varchar(50) );

I want to prepare and execute in binary mode:
select name from foo where id in ($1);

Execute works when I have a single value for $1, and treat it as a
normal INT4. However, when I attempt to send multiple values for the
parameter, I get no meaningful results.

My code snippets are below.

When setting up an array of numbers to pass as a parameter, is there
something special that needs to be done? The documentation is quite
vague; I did poke around the source and found in contrib some int_array
code, but don't fully understand it.

I suspect I need to do something like ...
(a) when I prepare, do something to tell postgres that I will have an
array of values, instead of a single value, and/or
(b) perhaps encapsulate the array of integers in some manner.

If I could be pointed to an example or documentation, it would be much
appreciated.

Thanks,
Matt Sanchez

The prepare code snippet:

Oid oids[1] = { 23 }; //INT4OID

result = PQprepare( pgconn, "getname",
"select name from foo where id in ($1)"
1, oids );

The execute code snippet:

int ids[4] = { 3, 5, 6, 8 }; // param values we want to setup

/* convert numbers to network byte order ... */

char * paramvalues[1] = (char *) ids;
int paramlengths[1];
int paramformats[1] = { 1 };

paramlengths[0] = = 4 * sizeof( int );

result = PQexecPrepared( pgconn,
"getname", // statement name
1, // number of params
paramvalues,
paramlenths,
paramformats,
1 );

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nick 2009-11-13 07:10:09 Can anyone help setting up pgbouncer?
Previous Message Joshua J. Kugler 2009-11-13 04:32:24 Re: Postgres Clustering Options