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 1khz5s-0004UR-Gv for psycopg@arkaria.postgresql.org; Wed, 25 Nov 2020 18:00:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khz5r-0006wd-AD for psycopg@arkaria.postgresql.org; Wed, 25 Nov 2020 18:00:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khz5r-0006wV-4m for psycopg@lists.postgresql.org; Wed, 25 Nov 2020 18:00:39 +0000 Received: from hl856.dinaserver.com ([82.98.132.60]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khz5o-0006lZ-2i for psycopg@postgresql.org; Wed, 25 Nov 2020 18:00:37 +0000 X-Spam-Status: No X-DinaScanner-From: listas@soft-com.es X-DinaScanner-SpamCheck: no es spam, SpamAssassin (no almacenado, puntaje=0.8, requerido 6, autolearn=disabled, ALL_TRUSTED -1.00, BAYES_50 1.80) X-DinaScanner: Libre de Virus X-DinaScanner-ID: CA0BC47E6C4F.A3D76 X-DinaScanner-Information: DinaScanner. Filtro anti-Spam y anti-Virus Received: from [192.168.0.103] (unknown [188.215.11.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: oswaldo@soft-com.es) by hl856.dinaserver.com (Postfix) with ESMTPSA id CA0BC47E6C4F; Wed, 25 Nov 2020 19:00:21 +0100 (CET) Subject: Re: Adaptation in psycopg3 To: Daniele Varrazzo Cc: psycopg@postgresql.org References: <7ec5da30-9d28-2bab-7eaa-7038711a99f0@soft-com.es> From: listas Message-ID: <9d1b2ad5-37a3-ebce-a622-24c6a1f5e1b4@soft-com.es> Date: Wed, 25 Nov 2020 19:00:21 +0100 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk El 25/11/20 a las 15:45, Daniele Varrazzo escribió: > 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 > Thank for your replies, I will use "=any(params list)" in psycopg3 The second question is: if psycopg3 is going to do the automatic cast of the types, will it be able to distinguish between a json and a list of values?. Example: data = ["a", "b", "c"] idList = [4,7,2] cursor.execute("update mytable set jsfield=%s where id = any(%s)", (data, idList)) What will be the correct syntax in this case? Thanks, -- Oswaldo Hernández