Python 2.3 Breaks PySQLite a Little

A

achrist

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
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

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.
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Greg said:
Gerhard Häring said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top