Re: Inserting variable into - Mailing list psycopg

From Adrian Klaver
Subject Re: Inserting variable into
Date
Msg-id 63f9baec-b185-3278-e8a7-84692579f9d8@aklaver.com
Whole thread Raw
In response to RE: Inserting variable into  (<hagen@datasundae.com>)
Responses RE: Inserting variable into  (<hagen@datasundae.com>)
BACK: Inserting a variable into cur.execute statement  (Hagen Finley <hagen@datasundae.com>)
List psycopg
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 <adrian.klaver@aklaver.com>
> 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



psycopg by date:

Previous
From:
Date:
Subject: RE: Inserting variable into
Next
From:
Date:
Subject: RE: Inserting variable into