public inbox for [email protected]  
help / color / mirror / Atom feed
Inserting variable into
11+ messages / 5 participants
[nested] [flat]

* Inserting variable into
@ 2020-12-07 22:02  [email protected]
  0 siblings, 1 reply; 11+ messages in thread

From: [email protected] @ 2020-12-07 22:02 UTC (permalink / raw)
  To: [email protected]; psycopg

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






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Inserting variable into
@ 2020-12-07 22:03  Adrian Klaver <[email protected]>
  parent: [email protected]
  0 siblings, 1 reply; 11+ messages in thread

From: Adrian Klaver @ 2020-12-07 22:03 UTC (permalink / raw)
  To: [email protected]; [email protected]; psycopg

On 12/7/20 2:02 PM, [email protected] 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
[email protected]





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* RE: Inserting variable into
@ 2020-12-07 22:26  [email protected]
  parent: Adrian Klaver <[email protected]>
  0 siblings, 1 reply; 11+ messages in thread

From: [email protected] @ 2020-12-07 22:26 UTC (permalink / raw)
  To: 'Adrian Klaver' <[email protected]>; [email protected]; psycopg

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.

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

On 12/7/20 2:02 PM, [email protected] 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
[email protected]








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Inserting variable into
@ 2020-12-07 22:31  Adrian Klaver <[email protected]>
  parent: [email protected]
  0 siblings, 2 replies; 11+ messages in thread

From: Adrian Klaver @ 2020-12-07 22:31 UTC (permalink / raw)
  To: [email protected]; [email protected]; psycopg

On 12/7/20 2:26 PM, [email protected] 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 <[email protected]>
> Sent: Monday, December 7, 2020 3:04 PM
> To: [email protected]; [email protected]; [email protected]
> Subject: Re: Inserting variable into
> 
> On 12/7/20 2:02 PM, [email protected] 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
[email protected]





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* RE: Inserting variable into
@ 2020-12-07 22:34  [email protected]
  parent: Adrian Klaver <[email protected]>
  1 sibling, 0 replies; 11+ messages in thread

From: [email protected] @ 2020-12-07 22:34 UTC (permalink / raw)
  To: 'Adrian Klaver' <[email protected]>; [email protected]; psycopg

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

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

On 12/7/20 2:26 PM, [email protected] 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 <[email protected]>
> Sent: Monday, December 7, 2020 3:04 PM
> To: [email protected]; [email protected]; 
> [email protected]
> Subject: Re: Inserting variable into
> 
> On 12/7/20 2:02 PM, [email protected] 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-quer
> ies
> 
>>
>> I imagine others do  - thanks!
>>
>> Best,
>>
>> Hagen
>>
>>
>>
> 
> 


--
Adrian Klaver
[email protected]








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* BACK: Inserting a variable into cur.execute statement
@ 2020-12-20 23:13  Hagen Finley <[email protected]>
  parent: Adrian Klaver <[email protected]>
  1 sibling, 2 replies; 11+ messages in thread

From: Hagen Finley @ 2020-12-20 23:13 UTC (permalink / raw)
  To: Adrian Klaver <[email protected]>; [email protected]; psycopg

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:
> On 12/7/20 2:26 PM, [email protected] 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 <[email protected]>
>> Sent: Monday, December 7, 2020 3:04 PM
>> To: [email protected]; [email protected]; 
>> [email protected]
>> Subject: Re: Inserting variable into
>>
>> On 12/7/20 2:02 PM, [email protected] 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
>>>
>>>
>>>
>>
>>
>
>


^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BACK: Inserting a variable into cur.execute statement
@ 2020-12-20 23:33  Daniele Varrazzo <[email protected]>
  parent: Hagen Finley <[email protected]>
  1 sibling, 1 reply; 11+ messages in thread

From: Daniele Varrazzo @ 2020-12-20 23:33 UTC (permalink / raw)
  To: Hagen Finley <[email protected]>; +Cc: Adrian Klaver <[email protected]>; [email protected]; psycopg

> 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 protected]> 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 <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:
>
> On 12/7/20 2:26 PM, [email protected] 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 <[email protected]>
> Sent: Monday, December 7, 2020 3:04 PM
> To: [email protected]; [email protected]; [email protected]
> Subject: Re: Inserting variable into
>
> On 12/7/20 2:02 PM, [email protected] 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
>
>
>
>
>
>
>





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BACK: Inserting a variable into cur.execute statement
@ 2020-12-21 00:09  Hagen Finley <[email protected]>
  parent: Daniele Varrazzo <[email protected]>
  0 siblings, 0 replies; 11+ messages in thread

From: Hagen Finley @ 2020-12-21 00:09 UTC (permalink / raw)
  To: Daniele Varrazzo <[email protected]>; +Cc: Adrian Klaver <[email protected]>; [email protected]; psycopg

Thank you Daniele,

That did it! Much appreciated.

Best,

Hagen

On 12/20/20 4:33 PM, Daniele Varrazzo wrote:
>> 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 protected]> 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 <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:
>>
>> On 12/7/20 2:26 PM, [email protected] 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 <[email protected]>
>> Sent: Monday, December 7, 2020 3:04 PM
>> To: [email protected]; [email protected]; [email protected]
>> Subject: Re: Inserting variable into
>>
>> On 12/7/20 2:02 PM, [email protected] 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
>>
>>
>>
>>
>>
>>
>>





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BACK: Inserting a variable into cur.execute statement
@ 2020-12-21 00:57  Adrian Klaver <[email protected]>
  parent: Hagen Finley <[email protected]>
  1 sibling, 1 reply; 11+ messages in thread

From: Adrian Klaver @ 2020-12-21 00:57 UTC (permalink / raw)
  To: Hagen Finley <[email protected]>; [email protected]; psycopg

On 12/20/20 3:13 PM, 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 pin param:
>      def_acct_analysis(param[par][0], param[par][1])
> 
>      par +=1


FYI, the above can be simplified to:

param = [(1,'ACCT0'),(2,'ACCT1'),(3,'ACCT2'),]

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

> 
> #Print statements above output:
> 

-- 
Adrian Klaver
[email protected]





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* RE: BACK: Inserting a variable into cur.execute statement
@ 2020-12-21 01:21  [email protected]
  parent: Adrian Klaver <[email protected]>
  0 siblings, 1 reply; 11+ messages in thread

From: [email protected] @ 2020-12-21 01:21 UTC (permalink / raw)
  To: 'Adrian Klaver' <[email protected]>; [email protected]; psycopg

Interesting - thank you Adrian.

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

On 12/20/20 3:13 PM, 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 pin param:
>      def_acct_analysis(param[par][0], param[par][1])
> 
>      par +=1


FYI, the above can be simplified to:

param = [(1,'ACCT0'),(2,'ACCT1'),(3,'ACCT2'),]

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

> 
> #Print statements above output:
> 

--
Adrian Klaver
[email protected]






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BACK: Inserting a variable into cur.execute statement
@ 2020-12-21 02:44  Vladimir Ryabtsev <[email protected]>
  parent: [email protected]
  0 siblings, 0 replies; 11+ messages in thread

From: Vladimir Ryabtsev @ 2020-12-21 02:44 UTC (permalink / raw)
  To: [email protected]; +Cc: Adrian Klaver <[email protected]>; [email protected]; psycopg

> FYI, the above can be simplified to:
>
>     def_acct_analysis(p[0], p[1])

Which, in turn, can be simplified to

     def_acct_analysis(*p)

On Sun, 20 Dec 2020 at 17:21, <[email protected]> wrote:

> Interesting - thank you Adrian.
>
> -----Original Message-----
> From: Adrian Klaver <[email protected]>
> Sent: Sunday, December 20, 2020 5:57 PM
> To: Hagen Finley <[email protected]>; [email protected];
> [email protected]
> Subject: Re: BACK: Inserting a variable into cur.execute statement
>
> On 12/20/20 3:13 PM, 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 pin param:
> >      def_acct_analysis(param[par][0], param[par][1])
> >
> >      par +=1
>
>
> FYI, the above can be simplified to:
>
> param = [(1,'ACCT0'),(2,'ACCT1'),(3,'ACCT2'),]
>
> for p in param:
>      def_acct_analysis(p[0], p[1])
>
> >
> > #Print statements above output:
> >
>
> --
> Adrian Klaver
> [email protected]
>
>
>
>


^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2020-12-21 02:44 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-07 22:02 Inserting variable into [email protected]
2020-12-07 22:03 ` Adrian Klaver <[email protected]>
2020-12-07 22:26   ` [email protected]
2020-12-07 22:31     ` Adrian Klaver <[email protected]>
2020-12-07 22:34       ` [email protected]
2020-12-20 23:13       ` BACK: Inserting a variable into cur.execute statement Hagen Finley <[email protected]>
2020-12-20 23:33         ` Re: BACK: Inserting a variable into cur.execute statement Daniele Varrazzo <[email protected]>
2020-12-21 00:09           ` Re: BACK: Inserting a variable into cur.execute statement Hagen Finley <[email protected]>
2020-12-21 00:57         ` Re: BACK: Inserting a variable into cur.execute statement Adrian Klaver <[email protected]>
2020-12-21 01:21           ` RE: BACK: Inserting a variable into cur.execute statement [email protected]
2020-12-21 02:44             ` Re: BACK: Inserting a variable into cur.execute statement Vladimir Ryabtsev <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox