It should be simple, bit I’ve spent the last hour searching for the answer. This is using psycopg2 on python 2.6.
I need something like this:
special_id = 5
sql = """
select count(*) as ct,
from some_table tbl
where tbl.id = %(the_id)
"""
cursor = connection.cursor()
cursor.execute(sql, {"the_id" : special_id})
I cannot get this to work. Were special_id a string, I could replace %(the_id) with %(the_id)s and things work well. However, I want it to use the integer so that it hits my indexes correctly.
There is a surprising lack of specific information on psycopg2 on the internet. I hope someone has an answer to this seemingly simple question.
Per PEP 249, since in psycopg2
paramstyleispyformat, you need to use%(the_id)seven for non-strings — trust it to do the right thing.BTW, internet searches will work better if you use the correct spelling (no
hthere), but even if you mis-spelled, I’m surprised you didn’t get a “did you mean” hint (I did when I deliberately tried!).