An introduction to the standard interface for Pythonic database interactions.
Despite the norms of SQL, individual databases have lots of differences.
Programmers don’t want to have to think about implementation details for underlying systems.
It would be nice to have a single API to hide these details.
Any package implementing this API would then be interchangeable.
Finalized in 1996, PEP 248 specified DB-API version 1.0 to fulfill this goal:
“This API has been defined to encourage similarity between the Python modules that are used to access databases. By doing this, we hope to achieve a consistency leading to more easily understood modules, code that is generally more portable across databases, and a broader reach of database connectivity from Python.”
source: http://www.python.org/dev/peps/pep-0248/
By 2001, PEP 249 brought version 2.0 of the DB-API specification, with improvements:
Discussions are currently underway to push DB-API v3.0, particularly in light of the changes in Python 3.0
source: http://www.python.org/dev/peps/pep-0249/
It is important to remember that PEP 249 is only a specification.
There is no code or package for DB-API 2.0 on it’s own.
Since 2.5, the Python Standard Library has provided a reference implementation of the api based on the SQLite3 RDBMS.
Before version 2.5, this package was available as pysqlite (and it still is).
To use the DB API with any database other than SQLite3, you must have an underlying API package available.
Implementations are available for:
source: http://wiki.python.org/moin/DatabaseInterfaces
Most db api packages can be installed using typical Pythonic methods:
$ pip install psycopg2
$ pip install mysql-python
...
Most api packages will require that the development headers for the underlying database system be available. Without these, the C symbols required for communication with the db are not present and the wrapper cannot work.
Moreover
Some of the db api wrappers have special installation requirements.
The MS SQL package runs only on Windows and requires pywin32. It is included in versions of pywin32 since v211.
The cx_Oracle package has binary installers, or can be installed from source using distutils:
$ python setup.py build
$ python setup.py install
What is in the DB API?
DB-API2 implementations provide the following global values:
These can be used to tailor your program’s expectations.
DB API provides a constructor connect, which returns a Connection object:
connect(parameters)
This can be considered the entry point for the module. Once you’ve got a connection, everything else flows from there.
The parameters required and accepted by the connect constructor will vary from implementation to implementation, since they are highly specific to the underlying database.
Some of the methods of the connection may not be supported by all implementations:
You can use a few values to control the rows returned by the cursor:
The final two methods may be implemented as no-ops
The cursor should be used to run operations on the database:
These attributes of Cursor can help you learn about the results of operations:
The return value .execute or .executemany is undefined and should not be used. These methods are the way to get results after an operation:
Note that each of these methods will raise a DB API Error if no operation has been performed (or if no result set was produced)
The DB-API provides types and constructors for data:
Each of the above has a corresponding <name>FromTicks(ticks) which returns the same type given a single integer argument (seconds since the epoch)
SQL NULL values are represented by Python’s None
The DB API specification requires implementations to create the following hierarchy of custom Exception classes:
StandardError
├──Warning
└──Error
├──InterfaceError (a problem with the db api)
└──DatabaseError (a problem with the database)
├──DataError (bad data, values out of range, etc.)
├──OperationalError (the db has an issue out of our control)
├──IntegrityError
├──InternalError
├──ProgrammingError (something wrong with the operation)
└──NotSupportedError (the operation is not supported)
Aside from some custom extensions not required by the specification, that’s it.