Package grizzled :: Package db
[frames] | no frames]

Package db

Introduction

The db module is a DB API wrapper. It provides a DB API-compliant API that wraps real underlying DB API drivers, simplifying some non-portable operations like connect() and providing some new operations.

Some drivers come bundled with this package. Others can be added on the fly.

Getting the List of Drivers

To get a list of all drivers currently registered with this module, use the get_driver_names() method:

import db

for driver_name in db.get_driver_names():
    print driver_name

Currently, this module provides the following bundled drivers:

Driver Name, as passed to get_driver() Database Underlying Python DB API module
dummy None db.DummyDB
gadfly Gadfly gadfly
mysql MySQL MySQLdb
oracle Oracle cx_Oracle
postgresql PostgreSQL psycopg2
sqlserver SQL Server pymssql
sqlite SQLite 3 sqlite3

To use a given driver, you must have the corresponding Python DB API module installed on your system.

Adding a Driver

It's possible to add a new driver to the list of drivers supplied by this module. To do so:

  1. The driver class must extend DBDriver and provide the appropriate methods. See examples in this module.
  2. The driver's module (or the calling program) must register the driver with this module by calling the add_driver() function.

DB API Factory Functions

The Binary(), Date(), DateFromTicks(), Time(), TimeFromTicks(), TimeStamp() and TimestampFromTicks() DB API functions can be found in the DB class. Thus, to make a string into a BLOB with this API, you use:

driver = db.get_driver(driver_name)
db = driver.connect(...)
blob = db.Binary(some_string)
Submodules

Functions
 
add_driver(key, driver_class, force=False)
Add a driver class to the list of drivers.
 
get_driver_names()
Get the list of driver names currently registered with this API.
DBDriver
get_driver(driver_name)
Get the DB API object for the specific database type.
Variables
  Warning
  Error
  DB
  DBError
  DBDriver
  Cursor
  apilevel = '2.0'
  threadsafety = '1'
  paramstyle = None
Function Details

add_driver(key, driver_class, force=False)

 
Add a driver class to the list of drivers.
Parameters:
  • key (str) - the key, also used as the driver's name
  • driver_class (class) - the DBDriver subclass object
  • force (bool) - True to force registration of the driver, even if there's an existing driver with the same key; False to throw an exception if there's an existing driver with the same key.
Raises:
  • ValueError - There's an existing driver with the same key, and force is False

get_driver_names()

 
Get the list of driver names currently registered with this API. Each of the returned names may be used as the first parameter to the get_driver() function.

get_driver(driver_name)

 
Get the DB API object for the specific database type. The list of legal database types are available by calling get_driver_names().
Parameters:
  • driver_name (str) - name (key) of the driver
Returns: DBDriver
the instantiated driver
Raises:
  • ValueError - Unknown driver name