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

From: silly8888 <silly8888(at)gmail(dot)com>
To: Matt Sanchez <matt-sanchez(at)comcast(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Libpq binary mode SELECT ... WHERE ID IN ($1) Question
Date: 2009-11-13 07:47:53
Message-ID: 3c8f9f940911122347kf2f7844h23e0bacf57965a03@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)comcast(dot)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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message weixiang tam 2009-11-13 09:16:28 Customize the install directory of the postgres DB
Previous Message Valtonen, Hannu 2009-11-13 07:44:35 Re: pgmemcache vs pgmemcached