5.2.3 draco.database -- the Database object

The Database object provides easy access to the Draco system database. The system database is used by Draco to implement some of its functionality and the can make use of it too. The details of connecting to the database, authentication and possibly connection persistence are all handled by Draco. A global instance of this object is stored in the current module under the name database.

Complementary to the database object, a module is available that provides access to the DB-API of the underlying database. This module also stored in draco.database, under the name dbapi. For MySQL databases this is the MySQLdb module, for PostgreSQL databases this is the psycopg module.

The following code fragment illustrates how to use these two objects:

from draco.database import database, dbapi

cursor = database.cursor()
try:
    cursor.execute('select * from orders')
except dbapi.Error:
    # error!

class Database( )
Global Draco object that provides access to the Draco system database.

The public methods or Database are:

connection( )
Returns the global database connection. The object returned is a Connection object that complies with the DB-API.

cursor( )
Return the global database cursor. The object returned is a Cursor object that complies with the DB-API.

commit( )
Commit any unfinished transaction on the global database connection. This is a short cut for connection().commit().

rollback( )
Roll back any unfinished transaction on the global database connection. This is a short cut for connection().rollback().