| From: | <hagen(at)datasundae(dot)com> | 
|---|---|
| To: | "'Adrian Klaver'" <adrian(dot)klaver(at)aklaver(dot)com>, <psycopg(at)lists(dot)postgresql(dot)org>, <psycopg(at)postgresql(dot)org> | 
| Subject: | RE: Inserting variable into | 
| Date: | 2020-12-07 22:26:26 | 
| Message-ID: | 0bf801d6cce8$011969d0$034c3d70$@datasundae.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | 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 <adrian(dot)klaver(at)aklaver(dot)com> 
Sent: Monday, December 7, 2020 3:04 PM
To: hagen(at)datasundae(dot)com; psycopg(at)lists(dot)postgresql(dot)org; psycopg(at)postgresql(dot)org
Subject: Re: Inserting variable into
On 12/7/20 2:02 PM, hagen(at)datasundae(dot)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(dot)klaver(at)aklaver(dot)com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Adrian Klaver | 2020-12-07 22:31:23 | Re: Inserting variable into | 
| Previous Message | Adrian Klaver | 2020-12-07 22:03:42 | Re: Inserting variable into |