sqlite3 adaptors mystery

Matej Cepl mcepl at redhat.com
Sun Mar 2 09:16:28 CET 2008

On 2008-03-01, 23:41 GMT, Mel wrote:
> There's nothing much wrong.  cur.fetchall is returning a list 
> of all the selected rows, and each row is a tuple of fields.  
> Each tuple is being converted for display by repr, so the 
> strings are shown as unicode, which is what they are 
> internally.  Change the print to
> for (field,) in cur.fetchall():
>      print field
> and you'll see your plain-text strings.

Thanks for your help, but plain-text strings is not what 
I wanted. The boolean variables was what I was after. See this 
modified version of the script:

import sqlite3
def adapt_boolean(bol):
     if bol:
             return "True"
             return "False"
def convert_boolean(bolStr):
     if str(bolStr) == "True":
             return bool(True)
     elif str(bolStr) == "False":
             return bool(False)
             raise ValueError, "Unknown value of bool attribute 
'%s'" % bolStr

db = sqlite3.connect(":memory:")
cur.execute("create table test(p boolean)")
cur.execute("insert into test(p) values (?)", (p,))
cur.execute("insert into test(p) values (?)", (p,))
cur.execute("select p from test")
for (field,) in cur.fetchall():
    print field,type(field)

The output here is:

[matej at viklef dumpBugzilla]$ python testAdaptors.py False <type 
True <type 'unicode'>
[matej at viklef dumpBugzilla]$ 

I thought that converter is there for just exactly this -- that 
I would get back bool values not strings.

Sorry for not being clear in the first run.


More information about the Python-list mailing list