Skip to main content

DB-API made easier

Project description

dbkit is a library that abstracts away at least part of the pain involved in dealing with DB-API 2 compatible database drivers.

Here’s an example:

from dbkit import connect, query
from contextlib import closing
import sqlite3

with connect(sqlite3, 'counters.db') as ctx, closing(ctx):
    for counter, value in query('SELECT counter, value FROM counters'):
        print "%s: %d" % (counter, value)


dbkit is intended to be used in circumstances where it is impractical or overkill to use an ORM such as SQLObject or SQLAlchemy, but it would be useful to at least abstract away some of the pain involved in dealing with the database.


  • Rather than passing around database connections, statements are executed within a database context, thus helping to decouple modules that interface with the database from the database itself and its connection details.
  • Database contexts contain references to the exceptions exposed by the database driver, thus decoupling exception handling from the database driver.
  • Easier to use transaction handling.
  • Easier iteration over resultsets.
  • Connection pooling. In addition, any code using pooled connections has no need to know connection pooling is in place.
  • Query logging.


  • Abstraction of SQL statements. The idea is to get rid of the more annoying but necessary boilerplate code involved in dealing with DB-API 2 drivers, not to totally abstract away SQL itself.


dbkit can be installed with either either pip (recommended):

$ pip install dbkit

Or with easy_install:

$ easy_install dbkit


The repos can be found on Github while the documentation can be found on Read the Docs.

Change history

0.2.4 (2015-11-30)

  • Python 3 support.

0.2.3 (2015-11-26)

  • Context.cursor() now always creates a transaction. The lack of this outer transaction meant that PostgreSQL would end up with a large number of idle transactions that had neither been committed or rolled back.

0.2.2 (2013-04-04)

  • Scrap unindent_statement().
  • Derive all dbkit exceptions from Exception.
  • Clean up connection pinging code.
  • Add make_placeholders() for generating statement placeholders safely.
  • Add to_dict() for converting resultsets to dicts mapped off of a particular field.

0.2.0 (2012-10-16)

  • Add last_row_id().
  • Pools now can have custom mediators.
  • Cursors are now tracked.
  • Pooled connections are no longer closed prematurely.
  • Row factories are now usable outside of context safely.

0.1.4 (2012-10-11)

  • execute*() now returns the number of affected rows.
  • Add last_row_count and last_row_id to Context.
  • Remove DummyPool and ThreadAffinePool, though the latter may be returning.
  • Stablise the behaviour of Pool when dealing with expired connections.
  • Documentation version is now pegged directly to the library.

0.1.2 (2012-09-02)

  • Initial revision with a changelog.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dbkit, version 0.2.4
Filename, size File type Python version Upload date Hashes
Filename, size dbkit-0.2.4-py2.py3-none-any.whl (12.1 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size dbkit-0.2.4.tar.gz (28.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page