Python 2.3 Breaks PySQLite a Little

Gerhard Häring gh at ghaering.de
Thu Aug 7 00:01:49 CEST 2003


achrist at easystreet.com wrote:
> Have hit a problem with PySQLite and python 2.3.  The new bool
> type doesn't want to get written properly to a database. I was
> expecting this to break when I started using True and False in 
> python 2.2, but it didn't.  Now there's a little trouble.  
> I changed the _quote function in pysqlite main.py to look like:
> 
> 
> 	def _quote(value):
> 
> 	    if value is None:
>         	return 'NULL'
> 	    elif isinstance(value, bool):   ### Added
>         	return 0 + value	    ### Added	
> 		[...]
> 
> Is that all it needs?

Yeah, except I'd write int(value). [1]

You'll also want to convert it back to bools, right? use the 
'converters' parameter of the connect call.

To check wether your changes worked as expected, you could use something 
like:

#v+
cx = sqlite.connect("db", converters={"bool": bool})
cu.execute("create table test(b bool)")
cu.execute("insert into test(b) values (%s)", (True,))
cu.execute("select b from test")
res = cu.fetchone()
assert type(res.b) is bool
#v-

All completely untested, I'm too lazy now ;-)

-- Gerhard

[1] And I intend to drop all this politically correct isinstance stuff 
in  a future version for performance reasons.





More information about the Python-list mailing list