sqlite3 adaptors mystery

Matej Cepl mcepl at redhat.com
Sat Mar 1 18:13:21 EST 2008


Hi,

I am in the process of creating a small script for filling the 
sqlite3 database with data from rather large XML-RPC query (list 
of many bugs from the Red Hat Bugzilla) and I would love to use 
adaptors and converters for some data types which I am missing.

I have this test script (made hopefully pretty faithfully from the
documentation):

#!/usr/bin/python
import sqlite3
def adapt_boolean(bol):
     if bol:
             return "True"
     else:
             return "False"
 
def convert_boolean(bolStr):
     if str(bolStr) == "True":
             return bool(True)
     elif str(bolStr) == "False":
             return bool(False)
     else:
             raise ValueError, "Unknown value of bool attribute '%s'" \
                % bolStr
 
sqlite3.register_adapter(bool,adapt_boolean)
sqlite3.register_converter("boolean",convert_boolean)

db = sqlite3.connect("test.db")
cur=db.cursor()
cur.execute("create table test(p boolean)")
p=False
cur.execute("insert into test(p) values (?)", (p,))
p=True
cur.execute("insert into test(p) values (?)", (p,))
cur.execute("select p from test")
print cur.fetchall()

And I would expect to print on output representation of bool values, i.e.,
something like

[False,True]

However, when running this program it seems converter doesn’t seem to work,
because I get:

[matej at viklef dumpBugzilla]$ rm test.db ; python testAdaptors.py
[(u'False',), (u'True',)]
[matej at viklef dumpBugzilla]$

There is probably something quite obvious what I do incorrectly, but I just
don't see it. Could somebody kick me in the right direction, please?

Thanks a lot,

Matěj Cepl



More information about the Python-list mailing list