skip to navigation
skip to content

sql 0.4.0

DB API 2.0 for Humans


DB API 2.0 works. ORMs are convenient but sometimes overkill. sql is a lightweight wrapper sitting on top of any DB API 2.0 connection offering a postgres like interface which makes working with SQL results bliss.


>>> import sqlite3
>>> connection = sqlite3.connect(':memory:')
>>> import sql
>>> bliss = sql.SQL(connection)


run is the method to use when you want to run a query but do not care about the result e.g. to create a table:

>>>"CREATE TABLE contributors (firstname VARCHAR, lastname VARCHAR)")
>>>"INSERT INTO contributors VALUES (?, ?)", [('Andrew', 'Kuchling'),
...                                                      ('James', 'Henstridge'),
...                                                      ('Daniele', 'Varrazzo'),
...                                                      ('Marc-Andre', 'Lemburg')])

Nothing impressive so far, creating a cursor and calling executemany would achieve the same result.


one is the method to use when you know the result is a single row or only care about one.

>>>"SELECT firstname FROM contributors WHERE lastname='Lemburg'") # doctest: +SKIP

The string, nothing but the string, which in my book beats:

>>> cursor = connection.cursor()
>>> cursor.execute("SELECT firstname FROM contributors WHERE lastname='Lemburg'") # doctest: +ELLIPSIS
<sqlite3.Cursor object at ...>
>>> cursor.fetchone() # doctest: +SKIP

Even better, if the result contains several column, one returns a namedtuple:

>>>"SELECT * FROM contributors WHERE firstname='James'") # doctest: +SKIP
Record(firstname=u'James', lastname=u'Henstridge')


all is the method to use to retrieve all rows from a query.

>>> bliss.all("SELECT firstname FROM contributors") #doctest: +SKIP
[u'Andrew', u'James', u'Daniele', u'Marc-Andre']

It returns a list of namedtuples when appropriate:

>>> bliss.all("SELECT firstname, LENGTH(lastname) AS length FROM contributors") # doctest: +NORMALIZE_WHITESPACE +SKIP
[Record(firstname=u'Andrew', length=8),
 Record(firstname=u'James', length=10),
 Record(firstname=u'Daniele', length=8),
 Record(firstname=u'Marc-Andre', length=7)]
File Type Py Version Uploaded on Size
sql-0.4.0.tar.gz (md5) Source 2014-01-06 3KB