Python 2.3 Breaks PySQLite a Little

Discussion in 'Python' started by achrist@easystreet.com, Aug 6, 2003.

  1. Guest

    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
    , Aug 6, 2003
    #1
    1. Advertising

  2. 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.
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Aug 6, 2003
    #2
    1. Advertising

  3. Greg Brunet wrote:
    > "Gerhard Häring" <> 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
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Aug 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Reedy
    Replies:
    0
    Views:
    400
    Terry Reedy
    May 19, 2004
  2. F. GEIGER
    Replies:
    2
    Views:
    529
    F. GEIGER
    May 21, 2005
  3. ThaDoctor
    Replies:
    3
    Views:
    372
    Alan Woodland
    Sep 28, 2007
  4. Gerhard Häring
    Replies:
    3
    Views:
    367
    Gerhard Häring
    Mar 13, 2008
  5. Daniel
    Replies:
    1
    Views:
    200
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page