sqlite3 docbug (was problem with sqlite3)

Terry Reedy tjreedy at udel.edu
Thu Jan 23 05:43:26 EST 2014


On 1/23/2014 12:35 AM, Rustom Mody wrote:
> On Thursday, January 23, 2014 10:11:42 AM UTC+5:30, Chris Angelico wrote:
>> I think it's fairly clear from the example that it has to be either a
>> tuple or a dict. Looks fine to me.
>
> yes 'from the example' and only from there!

'parameters' is a single parameter, which could be called 'seq_dict'. 
Let(seq_dict) must equal the number of replacements. A dict with extra 
pairs raises.

A list instead of a tuple does work, but not an iterable, so 'sequence'.

A dict subclass works, but a UserDict is treated as a sequence.
-----------
import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table people (name_last, age)")

who = "Yeltsin"
age = 72
s = (who, age)

d = {'who':who, 'age':age}

class D(dict): pass
dD = D(d)

from collections import UserDict
dU = UserDict(d)

# This is the qmark style:
cur.execute("insert into people values (?, ?)", s)

# And this is the named style:
cur.execute("select * from people where name_last=:who and age=:age", dU)

print(cur.fetchone())
--------------
 >>>
Traceback (most recent call last):
   File "C:\Programs\Python34\tem.py", line 23, in <module>
     cur.execute("select * from people where name_last=:who and 
age=:age", dU)
   File "C:\Programs\Python34\lib\collections\__init__.py", line 883, in 
__getitem__
     raise KeyError(key)
KeyError: 0

Replacing dU in the last call with s works!

http://bugs.python.org/issue20364

-- 
Terry Jan Reedy




More information about the Python-list mailing list