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 1kmP3W-0008KL-RK for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:32:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmP3V-0000Zb-S2 for psycopg@arkaria.postgresql.org; Mon, 07 Dec 2020 22:32:29 +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 1kmP2Y-0006jG-1c for psycopg@lists.postgresql.org; Mon, 07 Dec 2020 22:31:30 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmP2U-00029J-EG for psycopg@lists.postgresql.org; Mon, 07 Dec 2020 22:31:29 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id A65685C01D4; Mon, 7 Dec 2020 17:31:24 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 07 Dec 2020 17:31:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=u 9r4qPTaJhYLZZ27lDpR8F/vVS9h6GpAJ0GflSEIV6k=; b=KjN9MAwLvT6BRi+rE f9NSchOlPb/6ZcAB/c3Das7EDczq/yWuo+3Gf5p2OvQTwqHpyC90a3843OUCvj9j Y3JOER6+9eamKEka/5WS/jfrUmBoEar6EJtyLbDyjrtZfd53wt1sfN7d5bNY7qNa 7nfeCLwa4HlXPJDcnt/djIgWVGGyDkY8KVAAIu7hyqkqV2VFQDVnvA90CQpoxP7m pZEpj2fnnSquJfNHz8ixDWIthHrj+wwJJfVStWysv4urV/jGK0Dx9Pn0yBwdNNE6 xn/EJAQCn6Bt26daTVEowq6oRsdluqLmEVhFtdYkOquu52FB0sHBRbFyk+TKtBAs 6AUFg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=u9r4qPTaJhYLZZ27lDpR8F/vVS9h6GpAJ0GflSEIV 6k=; b=F19OpiSvviO7Wn9qKXikgiYGXWTwUaY+6e+Z17hJQJVkUI7Ijg91pSlwy srSrQh1H1vVa9hxYiYDSv34/yMeOWpFwBGErWYvDPlXBUra5snz6VmRZTFSFm55z JymXuHB9wLfGy7u2YVH86iNOLvbXH9eNBX8eSLTIwphrDlfe7+TNAW5QMooyEm1p 7/OW9OPYI+wFjlzDHKxHMDMRQtkphBe/+WdCPGwCzTK1uxIQUIDGWbOANiZdCoX8 F1/uTCdecEuxHILXz3VilEVEU/6Sv5V5oKvwos1Oo4GmSCH9q7BD70bYLGc4D1wm VHcfi0JNu3BUNM9FlTqLnDuB9bWfg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudejgedgudeivdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ejredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeejgfeiie eivdeuheevkeekkeejvedtgeeihffhvdffheeiheeltdekveetieehjeenucffohhmrghi nhepphhshigtohhpghdrohhrghenucfkphepjeehrddujedvrddurdeileenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-1-69.tukw.qwest.net [75.172.1.69]) by mail.messagingengine.com (Postfix) with ESMTPA id 06EC8108005F; Mon, 7 Dec 2020 17:31:23 -0500 (EST) Subject: Re: Inserting variable into To: hagen@datasundae.com, psycopg@lists.postgresql.org, psycopg@postgresql.org References: <0bef01d6cce4$987d8120$c9788360$@datasundae.com> <5c55fb91-0b03-2077-fce7-38f2dcc0d402@aklaver.com> <0bf801d6cce8$011969d0$034c3d70$@datasundae.com> From: Adrian Klaver Message-ID: <63f9baec-b185-3278-e8a7-84692579f9d8@aklaver.com> Date: Mon, 7 Dec 2020 14:31:23 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: <0bf801d6cce8$011969d0$034c3d70$@datasundae.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/7/20 2:26 PM, hagen@datasundae.com wrote: > So if I understand this correctly my new cur.execute would read: > > account = 'JPMC' > > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = %s AND 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):" > > and that would translate to > > cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'JPMC' AND stage LIKE 'Commit%';") > > is that right? > > Not sure what below is supposed to be about? > > Note You can use a Python list as the argument of the IN operator using the PostgreSQL ANY operator. > ids = [10, 20, 30] > cur.execute("SELECT * FROM data WHERE id = ANY(%s);", (ids,)) > Furthermore ANY can also work with empty lists, whereas IN () is a SQL syntax error. > > -----Original Message----- > From: Adrian Klaver > 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, >> >> I'd like to use a variable for 'Big Company' (e.g. account) or where = statements generally in my cur.execute statements: >> >> cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'Big Company' AND stage LIKE 'Commit%';") >> commitd1 = cur.fetchone() >> conn.commit() >> >> 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 > >> >> I imagine others do - thanks! >> >> Best, >> >> Hagen >> >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com