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

Package db

source code

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 [hide private]

Classes [hide private]
  Cursor
Class for DB cursors returned by the DB.cursor() method.
  DB
The object returned by a call to DBDriver.connect().
  DBDriver
Base class for all DB drivers.
  DBError
Base class for all DB exceptions.
  Error
Thrown to indicate an error in the db module.
  Warning
Thrown to indicate an error in the db module.
Functions [hide private]
 
add_driver(key, driver_class, force=False)
Add a driver class to the list of drivers.
source code
list
get_drivers()
Get the list of drivers currently registered with this API.
source code
 
get_driver_names()
Get the list of driver names currently registered with this API.
source code
DBDriver
get_driver(driver_name)
Get the DB API object for the specific database type.
source code
Variables [hide private]
  drivers = {'dummy': 'DummyDriver', 'gadfly': 'GadflyDriver', '...
  apilevel = '2.0'
  threadsafety = '1'
  paramstyle = None
hash(x)
  __package__ = 'grizzled.db'
Function Details [hide private]

add_driver(key, driver_class, force=False)

source code 
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_drivers()

source code 

Get the list of drivers currently registered with this API. The result is a list of DBDriver subclasses. Note that these are classes, not instances. Once way to use the resulting list is as follows:

for driver in db.get_drivers():
    print driver.__doc__
Returns: list
list of DBDriver class names

get_driver_names()

source code 
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)

source code 
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

Variables Details [hide private]

drivers

Value:
{'dummy': 'DummyDriver',
 'gadfly': 'GadflyDriver',
 'mysql': 'MySQLDriver',
 'oracle': 'OracleDriver',
 'postgresql': 'PostgreSQLDriver',
 'sqlite': 'SQLite3Driver',
 'sqlserver': 'SQLServerDriver'}