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 1kmOxm-00085i-Bk for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:26:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmOxl-0006FM-AH for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:26:33 +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 1kmOxl-0006FF-4x for psycopg@lists.postgresql.org; Mon, 07 Dec 2020 22:26:33 +0000 Received: from p3plsmtpa06-07.prod.phx3.secureserver.net ([173.201.192.108]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmOxi-00026s-CA for psycopg@postgresql.org; Mon, 07 Dec 2020 22:26:32 +0000 Received: from DESKTOPPBOKT6J ([98.38.100.21]) by :SMTPAUTH: with ESMTPSA id mOxfk1lEy2pnimOxfkIOQc; Mon, 07 Dec 2020 15:26:28 -0700 X-CMAE-Analysis: v=2.4 cv=QIWt+iHL c=1 sm=1 tr=0 ts=5fceac14 a=tZXnaF/PLMojNGgMD7Zm7A==:117 a=tZXnaF/PLMojNGgMD7Zm7A==:17 a=IkcTkHD0fZMA:10 a=SNL7wKpgAAAA:8 a=C-sdMEEzAAAA:8 a=epTmVMiNAAAA:8 a=nN1qTskwAAAA:8 a=EeyhsywC2xrkqAy_SAMA:9 a=QEXdDO2ut3YA:10 a=l8-UVUhtTF0A:10 a=Gua-9QImm5IA:10 a=uEXi9pYaZPHPrIM342IW:22 a=7WAne9lm5b0o3WrbkWlm:22 a=ndEWmUVY6Yapc0oHF_P4:22 a=nYCRw11HfzszU9rNbXoz:22 X-SECURESERVER-ACCT: hagen@datasundae.com From: To: "'Adrian Klaver'" , , References: <0bef01d6cce4$987d8120$c9788360$@datasundae.com> <5c55fb91-0b03-2077-fce7-38f2dcc0d402@aklaver.com> In-Reply-To: <5c55fb91-0b03-2077-fce7-38f2dcc0d402@aklaver.com> Subject: RE: Inserting variable into Date: Mon, 7 Dec 2020 15:26:26 -0700 Message-ID: <0bf801d6cce8$011969d0$034c3d70$@datasundae.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Content-Language: en-us Thread-Index: AQJo2ByQlIUQoUmlPw+s83H/ns7RlQIr5Xh8qLa+snA= X-CMAE-Envelope: MS4xfPwjsmGzK/d6S6/rboBLVz3+uOl5nFb6R0G8/CQh15aSbjvtl3HAGRKDu/sW88xeu5pG3bmiOdQKPzGlfyOvzLtVlUtYtmLm2LYWvjjzumM9CHCU0emB QCYa5V7xNR1q4iUL9GbsxSJUZyUrlnm1pFbzrf16BglMdUPknz0tpdfgvGaKp2fa7njJKIYCdg7R46w+yVHwLeVxyQyGVXIpkflaWUGcDFg6fS+v7idR84uk NjqYUgKotMLeHWRKxSVGqbb1aWAiq6qpoEIYY2B26ok= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk So if I understand this correctly my new cur.execute would read:=20 account =3D 'JPMC' cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D %s AND = stage LIKE 'Commit%';",(account )) and that would translate to=20 cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D 'JPMC' AND = stage LIKE 'Commit%';") is that right? Note You can use a Python list as the argument of the IN operator using = the PostgreSQL ANY operator. ids =3D [10, 20, 30] cur.execute("SELECT * FROM data WHERE id =3D ANY(%s);", (ids,)) Furthermore ANY can also work with empty lists, whereas IN () is a SQL = syntax error. -----Original Message----- From: Adrian Klaver =20 Sent: Monday, December 7, 2020 3:04 PM To: hagen@datasundae.com; psycopg@lists.postgresql.org; = psycopg@postgresql.org Subject: Re: Inserting variable into On 12/7/20 2:02 PM, hagen@datasundae.com wrote: > Hello, >=20 > I'd like to use a variable for 'Big Company' (e.g. account) or where = =3D statements generally in my cur.execute statements: >=20 > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D 'Big = Company' AND stage LIKE 'Commit%';") > commitd1 =3D cur.fetchone() > conn.commit() >=20 > but I don't know the proper syntax with the cur.execute statement to = use a variable. https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries= >=20 > I imagine others do - thanks! >=20 > Best, >=20 > Hagen >=20 >=20 >=20 --=20 Adrian Klaver adrian.klaver@aklaver.com