boodebr A little library from boodebr.org


files/feed-icon-14x14.png Recently Edited
boodebr.sql edit
frank, 21 June 2008 (created 03 October 2007)
Tags: sql pysqlite apsw
boodebr.sql is a light wrapper for pysqlite and APSW.

Use latest version of pysqlite
I recommend updating to the most recent version of pysqlite. The stock version bundled with Python 2.5 (pysqlite 2.3.2 with SQLite 3.3.4) appears to have issues under high loads. pysqlite 2.4.1 (with SQLite 3.5.2) seems stable.


See also: boodebr.sql.sqliteQ for easy multithreaded use.

Advantages over the standard DB-API 2.0 interface:
  • Wraps PyDB with iterators for nicer code
  • Iterator can return rows, dicts, or objects
  • Shorthand function names
  • When loading objects, calls obj.__postload__() so objects can perform their own SQL->object conversions.

I've divided functionality between the connection & cursor objects differently that the Python DB API does (i.e. query() is an attribute of the connection, not the cursor).

I think this leads to nicer code, and it allows cursors to be generated/used transparently.

For example, with Python DB API you'd do:
    sql = sqlite.connect( connect_args )
    cursor = sql.cursor()
    cursor.execute( query )

    row = cursor.fetchone()
    while row is not None:
         # do something with row
         ...
         row = cursor.fetchone()



With my wrapper, you can do things like:
    sql = fpmsql( connect_args )
    for row in sql.query( query ):
         # do something with row

    for dict_row in sql.query( query ).dict():
         # dict_row is row as dict where dict_row[colname] = value

    for obj in sql.query( query ).obj():
         # obj is row as an object (ie. obj.colname = value)



import * will import (fpmsql, DatabaseError)

NOTE
Recent versions of pysqlite have adopted iterator methods also, so you could use those directly, but I prefer my wrapper anyways for other niceties.