DB-API corner case (psycopg2)

Diez B. Roggisch deets at nospam.web.de
Fri Aug 1 10:39:14 EDT 2008


Diez B. Roggisch schrieb:
> Thomas Guettler schrieb:
>> Hi,
>>
>> I discovered this:
>>
>> import psycopg2
>> connection=psycopg2.connect("dbname='...' user='...'")
>> cursor=connection.cursor()
>> cursor.execute('''SELECT '%' ''') # Does not fail
>> cursor.execute('''SELECT '%' ''', ()) # Does fail
>>
>> Traceback (most recent call last):
>>   File "/localhome/modw/tmp/t.py", line 5, in <module>
>>     cursor.execute('''SELECT '%' ''', ()) # Does fail
>> IndexError: tuple index out of range
>>
>> Is this a bug in psycopg2?
>>
>> How do other PEP 249 implementation behave?
> 
> Not a bug. The second execute is the parametrized variant, which simply 
> tries to fetch the parameters from the passed collection. As you give an 
> empty collection, but specify one parameter, the error is quite obvious.
> 
> The first execute takes the SQL-string as literal.

On the second look, it appears that you don't acutally give a valid 
parameter specifier, just a fragment. However, in the same way

"%" % "foo"

yields ValueError - "incomplete format", this is bound to fail - you 
need to escape the % with %%.

Diez



More information about the Python-list mailing list