public inbox for [email protected]  
help / color / mirror / Atom feed
From: Daniele Varrazzo <[email protected]>
To: listas <[email protected]>
Cc: [email protected]
Subject: Re: Adaptation in psycopg3
Date: Wed, 25 Nov 2020 14:45:30 +0000
Message-ID: <CA+mi_8YBYfTjfhBvbz8ivRD4wz6d1MExjhWTcCHAXDiX3tKzeA@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CA+mi_8ZYBu5V1hcKvsnMqgKayGvyMRJAMdMQVXvZ02QXsnt9yA@mail.gmail.com>
	<[email protected]>

On Wed, 25 Nov 2020 at 12:29, listas <[email protected]> wrote:

> After reading the docs i have a question about the parameters in the
> 'in' clause.
>
> In psycopg2 i do:
>
>   params = (1,2,3,4)
>   cursor.execute("select * from mytable where field1 in %s", (params,))
>
> or
>
>   params = ('black','red','green')
>   cursor.execute("select * from mytable where field2 in %s", (params,))
>
> What will it be like in psycopg3, will it be the same?, will I have to
> create a special adapter?

Hollo Oswaldo,

"IN" cannot be used, because it's a SQL construct, so "(1, 2, 3)" is
not something that postgres will understand as a parameter.

You can use "= any (%s)" and pass a list. This is something you can do
in psycopg2 too, and it's actually a better choice, because it works
with empty lists too, unless `IN ()`, which is a syntax error for
Postgres.

What you can do is:

    params = ['black','red','green']
    cursor.execute("select * from mytable where field2 = any(%s)", (params,))

interesting fact: "= any" is what postgres really uses internally,
even if you use the "IN ()" syntax:

    piro=# explain select * from mytable where myint in (1,2,3);
                          QUERY PLAN
    ---------------------------------------------------------
    Seq Scan on mytable  (cost=0.00..45.06 rows=38 width=4)
      Filter: (myint = ANY ('{1,2,3}'::integer[]))
    (2 rows)

 -- Daniele





view thread (7+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Adaptation in psycopg3
  In-Reply-To: <CA+mi_8YBYfTjfhBvbz8ivRD4wz6d1MExjhWTcCHAXDiX3tKzeA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox