By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Python 2.3 Breaks PySQLite a Little

P: n/a
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
elif type(value) in (IntType, LongType, FloatType):
return value
elif isinstance(value, StringType):
return "'%s'" % value.replace("'", "''")
elif hasattr(value, '__quote__'):
return value.__quote__()
elif hasattr(value, '_quote'):
return value._quote()
elif have_datetime and type(value) in \
(DateTime.DateTimeType, DateTime.DateTimeDeltaType):
return "'%s'" % value
else:
return repr(value)
Is that all it needs?
Al
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ac*****@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.

Jul 18 '05 #2

P: n/a
Greg Brunet wrote:
"Gerhard Häring" <gh@ghaering.de> wrote:
[1] And I intend to drop all this politically correct isinstance stuff
in a future version for performance reasons.


Hey Gerhard:

What will you be using to replace it with that will improve the
performance? Is just doing a: type(value) that much faster?


Well, my plan is to rewrite PySQLite completely in C (maybe using PyRex).

You're right, it doesn't make a big difference if you use issubclass()
or type() in an if-elif chain. But using type() makes it possible to use
a dictionary to map types to quote functions, which *should* be faster.
Note the "should" ;-)

I'll have to benchmark a little more. However I really want to add the
feature of being able to register new quote functions without
subclassing the type in question and adding a _quote() method, which is
currently the only way. Apart from directly hacking the PySQLite
sources, like you did ;-)

-- Gerhard

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.