473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Python 2.3 Breaks PySQLite a Little

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
2 1683
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Martin Maney | last post by:
Apologies if this isn't news here - I've been too busy this last week or so for even skimming the traffic here, in part because I've been messing around with Ubuntu's preview release on a spare...
11
by: Gerhard Haering | last post by:
Last December, we had a short thread discussing (in python-dev) the integration of PySQLite into Python 2.4. At the time, I was against inclusion, because I thought PySQLite was not ripe for it,...
6
by: Simon Brunning | last post by:
QOTW: "And what defines a 'python activist' anyway? Blowing up Perl installations worldwide?" - Ivan Van Laningham "Floating point is about nothing if not being usefully wrong." - Robert Kern ...
8
by: Vittorio | last post by:
I am reading "Beginning Python from Novice to Professional" and the book is really awesome. Nonetheless on ch 13 "Database Support" I found this code to import data (in a txt file) into a SQLite...
0
by: Guilherme Polo | last post by:
On Tue, Jul 1, 2008 at 9:51 PM, Joe Goldthwaite <joe@goldthwaites.comwrote: You need sqlite itself (www.sqlite.org) and bindings, pysqlite 2 (http://oss.itsystementwicklung.de/trac/pysqlite/) or...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.