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.
The interface specification consists of several sections:
Comments and questions about this specification may be directed to the SIG for Database Interfacing with Python.
For more information on database interfacing with Python and available packages see the Database Topics Guide on www.python.org.
This document describes the Python Database API Specification 2.0. The previous version 1.0 version is still available as reference. Package writers are encouraged to use this version of the specification as basis for new interfaces.
Access to the database is made available through connection objects. The module must provide the following constructor for these:
These module globals must be defined:
'1.0'
and
'2.0'
are allowed.
If not given, a Database API 1.0 level interface should be assumed.
0 |
= Threads may not share the module. |
1 |
= Threads may share the module, but not connections. |
2 |
= Threads may share the module and connections. |
3 |
= Threads may share the module, connections and cursors. |
Sharing in the above context means that two threads may use a resource without wrapping it using a mutex semaphore to implement resource locking. Note that you cannot always make external resources thread safe by managing access using a mutex: the resource may rely on global variables or other external sources that are beyond your control.
'qmark' |
= Question mark style, e.g. '...WHERE name=?' |
'numeric' |
= Numeric, positional style, e.g. '...WHERE name=:1' |
'named' |
= Named style, e.g. '...WHERE name=:name' |
'format' |
= ANSI C printf format codes, e.g. '...WHERE name=%s' |
'pyformat' |
= Python extended format codes, e.g. '...WHERE name=%(name)s' |
The module should make all error information available through these exceptions or subclasses thereof:
This is the exception inheritance layout:
StandardError |__Warning |__Error |__InterfaceError |__DatabaseError |__DataError |__OperationalError |__IntegrityError |__InternalError |__ProgrammingError |__NotSupportedError
Note: The values of these exceptions are not defined. They should give the user a fairly good idea of what went wrong though.
Connections Objects should respond to the following methods:
Error
(or subclass) exception
will be raised if any operation is attempted with the
connection. The same applies to all cursor objects trying
to use the connection.
Database modules that do not support transactions should implement this method with void functionality.
In case a database does provide transactions this method causes the the database to roll back to the start of any pending transaction. Closing a connection without committing the changes first will cause an implicit rollback to be performed.
These objects represent a database cursor, which is used to manage the context of a fetch operation.
Cursor Objects should respond to the following methods and attributes:
(name, type_code, display_size,
internal_size, precision, scale, null_ok)
. This
attribute will be None
for operations that do not
return rows or if the cursor has not had an operation invoked
via the executeXXX()
method yet.
The type_code
can be interpreted by
comparing it to the Type Objects
specified in the section below.
executeXXX()
produced (for DQL
statements like select) or affected (for DML
statements like update or insert).
The attribute is -1 in case no executeXXX()
has been performed on the cursor or the rowcount of the
last operation is not determinable by the interface.[7]
Call a stored database procedure with the given name. The sequence of parameters must contain one entry for each argument that the procedure expects. The result of the call is returned as modified copy of the input sequence. Input parameters are left untouched, output and input/output parameters replaced with possibly new values.
The procedure may also provide a result set as
output. This must then be made available through the
standard fetchXXX()
methods.
Error
(or subclass) exception
will be raised if any operation is attempted with the
cursor.
paramstyle
attribute for
details). [5]
A reference to the operation will be retained by the cursor. If the same operation object is passed in again, then the cursor can optimize its behavior. This is most effective for algorithms where the same operation is used, but different parameters are bound to it (many times).
For maximum efficiency when reusing an operation, it is best to use the setinputsizes() method to specify the parameter types and sizes ahead of time. It is legal for a parameter to not match the predefined information; the implementation should compensate, possibly with a loss of efficiency.
The parameters may also be specified as list of tuples
to e.g. insert multiple rows in a single operation, but
this kind of usage is depreciated:
executemany()
should be used instead.
Return values are not defined.
seq_of_parameters
.
Modules are free to implement this method using multiple
calls to the execute()
method or by using
array operations to have the database process the
sequence as a whole in one call.
The same comments as for execute()
also
apply accordingly to this method.
Return values are not defined.
None
when no more data is available.
[6]
An Error
(or subclass) exception is raised
if the previous call to executeXXX()
did
not produce any result set or no call was issued yet.
The number of rows to fetch per call is specified by the
parameter. If it is not given, the cursor's
arraysize
determines the number of rows to
be fetched. The method should try to fetch as many rows
as indicated by the size parameter. If this is not
possible due to the specified number of rows not being
available, fewer rows may be returned.
An Error
(or subclass) exception is raised
if the previous call to executeXXX()
did
not produce any result set or no call was issued yet.
Note there are performance considerations involved with
the size parameter. For optimal performance, it is
usually best to use the arraysize attribute. If the
size parameter is used, then it is best for it to retain
the same value from one fetchmany()
call to
the next.
arraysize
attribute
can affect the performance of this operation.
An Error
(or subclass) exception is raised
if the previous call to executeXXX()
did
not produce any result set or no call was issued yet.
This method will make the cursor skip to the next available set, discarding any remaining rows from the current set.
If there are no more sets, the method returns
None
. Otherwise, it returns a true value
and subsequent calls to the fetch methods will return
rows from the next result set.
An Error
(or subclass) exception is raised
if the previous call to executeXXX()
did
not produce any result set or no call was issued yet.
fetchmany()
. It defaults
to 1 meaning to fetch a single row at a time.
Implementations must observe this value with respect to
the fetchmany()
method, but are free to
interact with the database a single row at a time. It
may also be used in the implementation of
executemany()
.
executeXXX()
to predefine memory areas for
the operation's parameters.
sizes
is specified as a sequence -- one
item for each input parameter. The item should be a
Type Object that corresponds to the input that will be
used, or it should be an integer specifying the maximum
length of a string parameter. If the item is
None
, then no predefined memory area will
be reserved for that column (this is useful to avoid
predefined areas for large inputs).
This method would be used before the
executeXXX()
method is invoked.
Implementations are free to have this method do nothing and users are free to not use it.
This method would be used before the
executeXXX()
method is invoked.
Implementations are free to have this method do nothing and users are free to not use it.
Many databases need to have the input in a particular format
for binding to an operation's input parameters. For
example, if an input is destined for a DATE column, then it
must be bound to the database in a particular string format.
Similar problems exist for "Row ID" columns or large binary
items (e.g. blobs or RAW columns). This presents problems
for Python since the parameters to the
executeXXX()
method are untyped. When the
database module sees a Python string object, it doesn't know
if it should be bound as a simple CHAR column, as a raw
BINARY item, or as a DATE.
To overcome this problem, a module must provide the constructors defined below to create objects that can hold special values. When passed to the cursor methods, the module can then detect the proper type of the input parameter and bind it accordingly.
A Cursor Object's description
attribute returns
information about each of the result columns of a query.
The type_code
must compare equal to one
of Type Objects defined below. Type Objects may be equal to
more than one type code (e.g. DATETIME could be equal to the
type codes for date, time and timestamp columns; see the Implementation Hints below for details).
The module exports the following constructors and singletons:
SQL NULL values are represented by the Python
None
singleton on input and output.
Note: Usage of Unix ticks for database interfacing can cause troubles because of the limited date range they cover.
import time def DateFromTicks(ticks): return apply(Date,time.localtime(ticks)[:3]) def TimeFromTicks(ticks): return apply(Time,time.localtime(ticks)[3:6]) def TimestampFromTicks(ticks): return apply(Timestamp,time.localtime(ticks)[:6])
class DBAPITypeObject: def __init__(self,*values): self.values = values def __cmp__(self,other): if other in self.values: return 0 if other < self.values: return 1 else: return -1
The resulting type object compares equal to all values passed to the constructor.
import exceptions class Error(exceptions.StandardError): pass class Warning(exceptions.StandardError): pass class InterfaceError(Error): pass class DatabaseError(Error): pass class InternalError(DatabaseError): pass class OperationalError(DatabaseError): pass class ProgrammingError(DatabaseError): pass class IntegrityError(DatabaseError): pass class DataError(DatabaseError): pass class NotSupportedError(DatabaseError): pass
In C you can use the PyErr_NewException(fullname,
base, NULL)
API to create the exception objects.
The Python Database API 2.0 introduces a few major changes compared to the 1.0 version. Because some of these changes will cause existing DB API 1.0 based scripts to break, the major version number was adjusted to reflect this change.
These are the most important changes from 1.0 to 2.0:
Although the version 2.0 specification clarifies a lot of questions that were left open in the 1.0 version, there are still some remaining issues:
dsn |
= Data source name as string | |
user |
= User name as string | (optional) |
password |
= Password as string | (optional) |
host |
= Hostname | (optional) |
database |
= Database name | (optional) |
E.g. a connect could look like this:
connect(dsn='myhost:MYDB',user='guido',password='234$?')
The preferred approach is to not implement the method and
thus have Python generate an AttributeError
in
case the method is requested. This allows the programmer to
check for database capabilities using the standard
hasattr()
function.
For some dynamically configured interfaces it may not be
appropriate to require dynamically making the method
available. These interfaces should then raise a
NotSupportedError
to indicate the non-ability
to perform the roll back when the method is invoked.
.fetchXXX()
methods.
The term "bound" refers to the process of binding an input value to a database execution buffer. In practical terms, this means that the input value is directly used as a value in the operation. The client should not be required to "escape" the value so that it can be used -- the value should be equal to the actual database value.
rowcount
attribute
may be coded in a way that updates its value dynamically. This
can be useful for databases that return useable rowcount
values only after the first call to a .fetchXXX()
method.