Subject: Inserting variable into

From: <[email-masked]>
Date: Mon, 7 Dec 2020 15:02:03 -0700

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. 

I imagine others do  - thanks!

Best,

Hagen

---

From: Adrian Klaver <[email-masked]>
Date: Mon, 7 Dec 2020 14:03:42 -0800

> 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

-- 
Adrian Klaver
[email-masked]

---

From: <[email-masked]>
Date: Mon, 7 Dec 2020 15:26:26 -0700

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 ))

and that would translate to 

cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = '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 = [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.

---

From: Adrian Klaver <[email-masked]>
Date: Mon, 7 Dec 2020 14:31:23 -0800

>   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):"

> cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'JPMC' AND stage LIKE 'Commit%';")

Not sure what below is supposed to be about?

---

From: <[email-masked]>
Date: Mon, 7 Dec 2020 15:34:00 -0700

OK got it. That's very helpful thank you!

-----Original Message-----
From: Adrian Klaver <[email-masked]> 
Sent: Monday, December 7, 2020 3:31 PM
To: [email-masked]; [email-masked]; [email-masked]
Subject: Re: Inserting variable into

> 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):"

---

From: Hagen Finley <[email-masked]>
Date: Sun, 20 Dec 2020 16:13:33 -0700

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 pin 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 <module>
> 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:
> 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):"
>
>
>
>>
>>> I'd like to use a variable for 'Big Company' (e.g. account) or where 
>>> = statements generally in my cur.execute statements:
>>>

---

From: Daniele Varrazzo <[email-masked]>
Date: Sun, 20 Dec 2020 23:33:02 +0000

> 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 <[email-masked]> 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.
>
>     sheet = "sheet"+str(sht)
> par = 0
> param = [(1,'ACCT0'),(2,'ACCT1'),(3,'ACCT2'),]
>
> for p in param:
>     par += 1
> 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()
>
>   File "/home/datasundae/PycharmProjects/Registration_Reports/sfdc_Account_Tab_Analysis_Function.py", line 333, in <module>
>   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
>

---

From: Hagen Finley <[email-masked]>
Date: Sun, 20 Dec 2020 17:09:02 -0700

Thank you Daniele,

That did it! Much appreciated.

Best,

Hagen

---

From: Adrian Klaver <[email-masked]>
Date: Sun, 20 Dec 2020 16:57:07 -0800

>      def_acct_analysis(param[par][0], param[par][1])

FYI, the above can be simplified to:

for p in param:
     def_acct_analysis(p[0], p[1])

---

From: <[email-masked]>
Date: Sun, 20 Dec 2020 18:21:11 -0700

Interesting - thank you Adrian.

-----Original Message-----
From: Adrian Klaver <[email-masked]> 
Sent: Sunday, December 20, 2020 5:57 PM
To: Hagen Finley <[email-masked]>; [email-masked]; [email-masked]
Subject: Re: BACK: Inserting a variable into cur.execute statement

for p in param:

---

From: Vladimir Ryabtsev <[email-masked]>
Date: Sun, 20 Dec 2020 18:44:24 -0800

>     def_acct_analysis(p[0], p[1])

Which, in turn, can be simplified to

     def_acct_analysis(*p)