Re: Libpq binary mode SELECT ... WHERE ID IN ($1) Question - Mailing list pgsql-general

From silly8888
Subject Re: Libpq binary mode SELECT ... WHERE ID IN ($1) Question
Date
Msg-id 3c8f9f940911122347kf2f7844h23e0bacf57965a03@mail.gmail.com
Whole thread Raw
In response to Libpq binary mode SELECT ... WHERE ID IN ($1) Question  (Matt Sanchez <matt-sanchez@comcast.net>)
List pgsql-general
You could try to prepare a query like this:

      select name from foo where id=any($1);

and then pass the array of integers as $1 (although, I don't know how
you can do that as I've never used the C interface of libpq).


On Fri, Nov 13, 2009 at 1:34 AM, Matt Sanchez <matt-sanchez@comcast.net> wrote:
> 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 );
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

pgsql-general by date:

Previous
From: Mikko Partio
Date:
Subject: Re: Postgres Clustering Options
Next
From: "Valtonen, Hannu"
Date:
Subject: Re: pgmemcache vs pgmemcached