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 1kr8D3-0006sM-4B for psycopg@arkaria.postgresql.org; Sun, 20 Dec 2020 23:33:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kr8CV-0005NX-5k for psycopg@arkaria.postgresql.org; Sun, 20 Dec 2020 23:33:19 +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 1kr8CU-0005NP-Us for psycopg@lists.postgresql.org; Sun, 20 Dec 2020 23:33:19 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kr8CR-0002IS-Pz for psycopg@postgresql.org; Sun, 20 Dec 2020 23:33:17 +0000 Received: by mail-lf1-x12a.google.com with SMTP id o17so19477926lfg.4 for ; Sun, 20 Dec 2020 15:33:15 -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=PSlf0S31Sb0W88rC8vT+XA1FbPW2sl6kgOt7CvGrWwQ=; b=GtwInddWq01v/TMiCWXNdQVKt8ew70WxStvfPb8GgUp2YM63v5tRwpPCDdatWBA4YF 4Zwqp9hHwVldK/qDkK5l5B3Or0dyE8BdUdBSpin6wuqOJisFlAHI+Aoy2WDnNzAJzXg5 t/bybtoUyj0O94qXTx+N2glQyrUyvk92mhz40g6nMO/K6rx7zM7xwl2bFpYNOKjM1rRx 3tnFdwJ80mwrvt6ZVYHfTLpAviM1+O0tYwDSQ2aqt17s8Yy3iE0AS6k2FLx94SVQf3+H SdhemuYFh4uPyJNy8If0WlBCXVHg9Hs+Xk52PpY5utn/Bo9pFHoIaIee9dC1XVGM2b13 JSVQ== 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=PSlf0S31Sb0W88rC8vT+XA1FbPW2sl6kgOt7CvGrWwQ=; b=Hj9BkqvmabaugGQQvwyHiun6P6E0YM1OICdatunvLwFHYFN40Oy29W3ID5yTjy/u5Z iu5SJxtwZBZMOQpFlLMP3E4C5GHHRGNOukHNkt4RjJkORSbxnGap8dFm1BgK8sNb04pr t/vY2syfsvaqZO3LtBn0wYXOQ3S1rs8f0YW+Obeaahy4THWEQwSR0eisU00WkYn7+e89 EV3cgi/7v6p8SFh/5XAgjazVZljQaeu2vV1REyL48hskhpQUD7Y63V448fhBhwn/C22i b9kyBfFZPMbE9t26UHjvMxIMD6TQnJe/m8f+D4ppTZ13AJKmSlOtUlg4cQOC39CTQZp5 KCPg== X-Gm-Message-State: AOAM531Ev/ZLFUymfk7stbLyFSjmlq1dFS3aTmDYauHi1USuUTQCkHNi hXthfeeeBi/Q5fFPJrCs0fAcIBnP8tFa2JWduHHRPUQ50EsnPg== X-Google-Smtp-Source: ABdhPJyFTJJXKVyfj/jbXjstDqjME+HEQAdStDwJ0eLZCOMusdnEAyHHLCf1s6WIxzXeM43kNCYp9Y/2SgGu2D1RxFk= X-Received: by 2002:a19:a57:: with SMTP id 84mr5826059lfk.327.1608507193927; Sun, 20 Dec 2020 15:33:13 -0800 (PST) MIME-Version: 1.0 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: From: Daniele Varrazzo Date: Sun, 20 Dec 2020 23:33:02 +0000 Message-ID: Subject: Re: BACK: Inserting a variable into cur.execute statement To: Hagen Finley Cc: Adrian Klaver , psycopg@lists.postgresql.org, psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > cur.execute("SELECT COALESCE(SUM(revusd),0) FROM sfdc where stage LIKE 'Win%' AND saccount = %s", (account,)) You have to escape the percent in the like as %%. -- Daniele On Sun, 20 Dec 2020 at 23:13, Hagen Finley wrote: > > Hello, > > I finally got around to trying to implement this code and I am running into an "IndexError: tuple index out of range" problem. > > I am running a function with parameters from a list: > > def def_acct_analysis(sht,acct): > print(param[par][0]) > print(param[par][1]) > sheet = "sheet"+str(sht) > print(sheet) > account = acct > print(account) > > par = 0 > param = [(1,'ACCT0'),(2,'ACCT1'),(3,'ACCT2'),] > > for p in param: > def_acct_analysis(param[par][0], param[par][1]) > > par += 1 > > #Print statements above output: > > 1 > ACCT0 > sheet1 > ACCT0 > > I want to insert the account name 'ACCT0' into my cur.execute but I get an error with this code: > > cur.execute("SELECT COALESCE(SUM(revusd),0) FROM sfdc where stage LIKE 'Win%' AND saccount = %s", (account,)) > wind1 = cur.fetchone() > conn.commit() > > Traceback (most recent call last): > File "/home/datasundae/PycharmProjects/Registration_Reports/sfdc_Account_Tab_Analysis_Function.py", line 333, in > def_acct_analysis(param[par][0], param[par][1]) > File "/home/datasundae/PycharmProjects/Registration_Reports/sfdc_Account_Tab_Analysis_Function.py", line 96, in def_acct_analysis > cur.execute("SELECT COALESCE(SUM(revusd),0) FROM sfdc where stage LIKE 'Win%' AND saccount = %s", (account,)) > IndexError: tuple index out of range > > I've returned to the psycopg docs but I don't see my error. Can someone else see it? > > Best, > > Hagen > > On 12/7/20 3:31 PM, Adrian Klaver wrote: > > 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 > > > > > > >