Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khw3G-0006jT-S8 for psycopg@arkaria.postgresql.org; Wed, 25 Nov 2020 14:45:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khw3F-0000qr-LZ for psycopg@arkaria.postgresql.org; Wed, 25 Nov 2020 14:45:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khw3F-0000qj-Dw for psycopg@lists.postgresql.org; Wed, 25 Nov 2020 14:45:45 +0000 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khw3D-0007Jf-45 for psycopg@postgresql.org; Wed, 25 Nov 2020 14:45:44 +0000 Received: by mail-lj1-x235.google.com with SMTP id r18so2576730ljc.2 for ; Wed, 25 Nov 2020 06:45:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=LdWmc1hlQ/WyUlzkow2YMsraIKnf+96brCusmiZMmec=; b=RvCdeBa77AGHVurlNL61shpzIeKp4YovlEagqocin+BCQj6MBN1wP38yfQ+P2ZotA9 Cjjt1Id+a6M9QbCrW0GB6v0odTTpTJPbpmAv+c+I/6QctuvcmmMDMSrjMJ59uBF7dyVK vne81LZTfKAdmpoI/n/XxsPqiok8VRY5P6ymZtSTUe/TAbbUulquuP6oh8XSJTXkKcAR Emoe/KNYXDgW+Nxb4CEyP/ZbNyINL52dcFYrJ9+r01tXOKPpmjaiGuV356e66baM/+Zb AhpX42o9jyuejbushErUpyafTpkRMzHvQIx7vmczBfCq32ukvCMuD1OexcuCtf4aGhUW CFjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=LdWmc1hlQ/WyUlzkow2YMsraIKnf+96brCusmiZMmec=; b=Wed7bop2AHLAepZOD0sP2SZc45cuuOBp1pTO7D1mGjz/JwzTBo5EXix++VuSYZbQCC 1Kl7YSGQZjkkfOv15NROmn9s09lmaRcdqzQ6bVEFsktr4KsGm/C1m2VTZixjk+y1++Se AHqz9tTmzaHtzzNLlyJ8jWfNkZhKj10sBTBPiASb8CreimWS8cZhNL0n2Puoelcs2Bpq heXPZr27jpAYkSGwXxR5A4gTyfoxh+ajzUTwAwex+oHmg/41UuKMOxb6Pj11RhuJLJqP RmSF0kxNWdj2vFf1TdzpONRaqAoqFlTlkncyF/pnioRHn4jwy1RVdU5KP+eqXRQg84ub E5Sg== X-Gm-Message-State: AOAM531buCGX5brkFcMywPbY10vXfgBwEXbYN4Qdj67b7m040nqkOkIS i64W/ljYpMrB9qYV/1e0YKpDK58EtBGy2aUU8TvgHyzMwY1DLA== X-Google-Smtp-Source: ABdhPJy+9HBRtMXAejddBiy8jv1fT9ogTFtSftG5N4cfn9UCkY3GdyfdWyMF7ukaxMN80p8YpRuuSo0pXR7TiRP4+3Y= X-Received: by 2002:a05:651c:1195:: with SMTP id w21mr1493595ljo.427.1606315542013; Wed, 25 Nov 2020 06:45:42 -0800 (PST) MIME-Version: 1.0 References: <7ec5da30-9d28-2bab-7eaa-7038711a99f0@soft-com.es> In-Reply-To: <7ec5da30-9d28-2bab-7eaa-7038711a99f0@soft-com.es> From: Daniele Varrazzo Date: Wed, 25 Nov 2020 14:45:30 +0000 Message-ID: Subject: Re: Adaptation in psycopg3 To: listas Cc: psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 25 Nov 2020 at 12:29, listas 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