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 1kmP55-0008NE-Rq for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:34:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmP54-0000eK-RW for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:34:06 +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 1kmP54-0000e6-Mc for psycopg@lists.postgresql.org; Mon, 07 Dec 2020 22:34:06 +0000 Received: from p3plsmtpa06-03.prod.phx3.secureserver.net ([173.201.192.104]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmP51-0002AO-Qs for psycopg@lists.postgresql.org; Mon, 07 Dec 2020 22:34:06 +0000 Received: from DESKTOPPBOKT6J ([159.100.160.43]) by :SMTPAUTH: with ESMTPSA id mP4ykDo3zCfzEmP4zkGQsy; Mon, 07 Dec 2020 15:34:01 -0700 X-CMAE-Analysis: v=2.4 cv=Au4rYMxP c=1 sm=1 tr=0 ts=5fceadd9 a=yh3KY/mAJQG2JE6OaVbeHA==:117 a=yh3KY/mAJQG2JE6OaVbeHA==:17 a=IkcTkHD0fZMA:10 a=SNL7wKpgAAAA:8 a=C-sdMEEzAAAA:8 a=epTmVMiNAAAA:8 a=nN1qTskwAAAA:8 a=yGq7AuJCQ8YcuOYrFasA: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> <0bf801d6cce8$011969d0$034c3d70$@datasundae.com> <63f9baec-b185-3278-e8a7-84692579f9d8@aklaver.com> In-Reply-To: <63f9baec-b185-3278-e8a7-84692579f9d8@aklaver.com> Subject: RE: Inserting variable into Date: Mon, 7 Dec 2020 15:34:00 -0700 Message-ID: <0bfa01d6cce9$0f614ed0$2e23ec70$@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/ns7RlQIr5Xh8AbQDjf0A356bEKiiJZGQ X-CMAE-Envelope: MS4xfNIhmj54YMZTAvgzoNG6METIlOkpcu4YKIMBugx/KWUINfFMSjebr947zm2jyFuNJGEz33n5qzlzhrRN3DtRzQdinnXVO7IRjiVFltGKG+9gmHTO+4oj kcFp2TE3gkv+gUsXWNKePHnCPqe74KULJac3atbyillzgCscJcB71or0q1XPuMV1bZjYlVxLbgk47UHfUz+q6gKtDKb1p+AHIowuDzg4Nn0iqThKW6EAsLoT Qd7Y2SWwvvHb/Q0+DreA3FCsiA2K2xY/71/eOHLP0r8= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk OK got it. That's very helpful thank you! -----Original Message----- From: Adrian Klaver =20 Sent: Monday, December 7, 2020 3:31 PM To: hagen@datasundae.com; psycopg@lists.postgresql.org; = psycopg@postgresql.org Subject: Re: Inserting variable into On 12/7/20 2:26 PM, hagen@datasundae.com wrote: > So if I understand this correctly my new cur.execute would read: >=20 > account =3D 'JPMC' >=20 > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D %s AND=20 > stage LIKE 'Commit%';",(account )) Since you are using a tuple this (account ) would need to be (account,) = per the docs at link previously posted: "For positional variables binding, the second argument must always be a = sequence, even if it contains a single variable (remember that Python = requires a comma to create a single element tuple):" >=20 > and that would translate to >=20 > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D 'JPMC' = AND=20 > stage LIKE 'Commit%';") >=20 > is that right? >=20 >=20 Not sure what below is supposed to be about? >=20 > 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,))=20 > Furthermore ANY can also work with empty lists, whereas IN () is a SQL = syntax error. >=20 > -----Original Message----- > From: Adrian Klaver > Sent: Monday, December 7, 2020 3:04 PM > To: hagen@datasundae.com; psycopg@lists.postgresql.org;=20 > psycopg@postgresql.org > Subject: Re: Inserting variable into >=20 > On 12/7/20 2:02 PM, hagen@datasundae.com wrote: >> Hello, >> >> I'd like to use a variable for 'Big Company' (e.g. account) or where = =3D statements generally in my cur.execute statements: >> >> cur.execute("SELECT SUM(revusd) FROM sfdc where saccount =3D 'Big=20 >> Company' AND stage LIKE 'Commit%';") >> commitd1 =3D cur.fetchone() >> conn.commit() >> >> but I don't know the proper syntax with the cur.execute statement to = use a variable. >=20 > https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-quer > ies >=20 >> >> I imagine others do - thanks! >> >> Best, >> >> Hagen >> >> >> >=20 >=20 -- Adrian Klaver adrian.klaver@aklaver.com