Python Database API Version 2.0 Concepts

As mentioned, the purpose of the Python Database API is to provide a database-neutral interface from Python to access data stored in database management systems.

The Python Database API Specification consists of the following sections:

Module Interface

Because access to RHDB is made available through Connection Objects, the Python module provides a constructor for the connection; the constructor returns a Connection Object proper. Also, the interface defines a set of global variables that describe the API. See the Section called Code: example.py for the about() method, which displays some of this information for the RHDB Python Database API implementation.

Connection Object

Once a connection has been established, you can use the Connection Object to handle transaction control (commits and rollbacks) and Cursor Object instantiation.

Cursor Objects

These objects represent database cursors. Cursor Object methods allow the user to query and manipulate database data and also describe the contents of the cursor itself.

Type Object and Constructors

Type Objects allow database-independent data types to be used when programming with the Python Database API. These types and constructors are mapped in the DB-API implementation to the underlying RHDB data types.

The following mappings are used between the API and Red Hat Database:

The following mappings use the mxDateTime package from eGenix.com:

Conceptual Level Example

Given the concepts from the Python Database API Specification, and taking an object-level view, the process of connecting to a database, inserting a row, and then disconnecting is as follows:

  1. Connect to the database by creating a Connection Object

    1. Use the Connection Object to create a Cursor Object

      1. Insert a row using the Cursor Object

      2. Close the Cursor Object

    2. Commit the transaction using the Connection Object

  2. Disconnect from the database using the Connection Object.