2001-03-29 12:00:15 -05:00
|
|
|
PEP: 248
|
|
|
|
Title: Python Database API Specification v1.0
|
2022-11-16 17:11:31 -05:00
|
|
|
Author: Greg Stein <gstein@lyra.org>, Marc-André Lemburg <mal@lemburg.com>
|
2007-06-28 16:11:32 -04:00
|
|
|
Discussions-To: db-sig@python.org
|
2002-05-29 17:17:42 -04:00
|
|
|
Status: Final
|
2001-03-29 12:00:15 -05:00
|
|
|
Type: Informational
|
2017-02-24 19:07:48 -05:00
|
|
|
Content-Type: text/x-rst
|
2022-11-16 17:11:31 -05:00
|
|
|
Created: 08-May-1996
|
2007-06-19 00:20:07 -04:00
|
|
|
Post-History:
|
2011-03-03 23:58:22 -05:00
|
|
|
Superseded-By: 249
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-02-24 19:07:48 -05:00
|
|
|
|
2001-03-29 12:00:15 -05:00
|
|
|
Introduction
|
2017-02-24 19:07:48 -05:00
|
|
|
============
|
|
|
|
|
|
|
|
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.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-02-24 19:07:48 -05:00
|
|
|
This interface specification consists of several items:
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-02-24 19:07:48 -05:00
|
|
|
* Module Interface
|
|
|
|
* Connection Objects
|
|
|
|
* Cursor Objects
|
|
|
|
* DBI Helper Objects
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-02-24 19:07:48 -05:00
|
|
|
Comments and questions about this specification may be directed to
|
|
|
|
the SIG on Tabular Databases in Python
|
|
|
|
(http://www.python.org/sigs/db-sig).
|
|
|
|
|
|
|
|
This specification document was last updated on: April 9, 1996.
|
|
|
|
It will be known as Version 1.0 of this specification.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
Module Interface
|
2017-02-24 19:07:48 -05:00
|
|
|
================
|
|
|
|
|
|
|
|
The database interface modules should typically be named with
|
|
|
|
something terminated by ``db``. Existing examples are: ``oracledb``,
|
|
|
|
``informixdb``, and ``pg95db``. These modules should export several
|
|
|
|
names:
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``modulename(connection_string)``
|
2017-02-24 19:07:48 -05:00
|
|
|
Constructor for creating a connection to the database.
|
|
|
|
Returns a Connection Object.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``error``
|
2017-02-24 19:07:48 -05:00
|
|
|
Exception raised for errors from the database module.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
Connection Objects
|
2017-02-24 19:07:48 -05:00
|
|
|
==================
|
|
|
|
|
|
|
|
Connection Objects should respond to the following methods:
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``close()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Close the connection now (rather than whenever ``__del__`` is
|
|
|
|
called). The connection will be unusable from this point
|
|
|
|
forward; an exception will be raised if any operation is
|
|
|
|
attempted with the connection.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``commit()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Commit any pending transaction to the database.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``rollback()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Roll the database back to the start of any pending
|
|
|
|
transaction.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``cursor()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Return a new Cursor Object. An exception may be thrown if
|
|
|
|
the database does not support a cursor concept.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``callproc([params])``
|
2017-02-24 19:07:48 -05:00
|
|
|
(Note: this method is not well-defined yet.) Call a
|
|
|
|
stored database procedure with the given (optional)
|
|
|
|
parameters. Returns the result of the stored procedure.
|
|
|
|
|
|
|
|
(all Cursor Object attributes and methods)
|
|
|
|
For databases that do not have cursors and for simple
|
|
|
|
applications that do not require the complexity of a
|
|
|
|
cursor, a Connection Object should respond to each of the
|
|
|
|
attributes and methods of the Cursor Object. Databases
|
|
|
|
that have cursor can implement this by using an implicit,
|
|
|
|
internal cursor.
|
|
|
|
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
Cursor Objects
|
2017-02-24 19:07:48 -05:00
|
|
|
==============
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``arraysize``
|
2017-02-24 19:07:48 -05:00
|
|
|
This read/write attribute specifies the number of rows to
|
|
|
|
fetch at a time with ``fetchmany()``. This value is also used
|
|
|
|
when inserting multiple rows at a time (passing a
|
|
|
|
tuple/list of tuples/lists as the params value to
|
|
|
|
``execute()``). This attribute will default to a single row.
|
|
|
|
|
|
|
|
Note that the arraysize is optional and is merely provided
|
|
|
|
for higher performance database interactions.
|
|
|
|
Implementations should observe it with respect to the
|
|
|
|
``fetchmany()`` method, but are free to interact with the
|
|
|
|
database a single row at a time.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``description``
|
2017-02-24 19:07:48 -05:00
|
|
|
This read-only attribute is a tuple of 7-tuples. Each
|
|
|
|
7-tuple contains information describing each result
|
|
|
|
column: (name, type_code, display_size, internal_size,
|
2017-04-05 12:14:26 -04:00
|
|
|
precision, scale, null_ok). This attribute will be ``None``
|
2017-02-24 19:07:48 -05:00
|
|
|
for operations that do not return rows or if the cursor
|
|
|
|
has not had an operation invoked via the ``execute()`` method
|
|
|
|
yet.
|
|
|
|
|
|
|
|
The 'type_code' is one of the 'dbi' values specified in
|
|
|
|
the section below.
|
|
|
|
|
|
|
|
Note: this is a bit in flux. Generally, the first two
|
|
|
|
items of the 7-tuple will always be present; the others
|
|
|
|
may be database specific.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``close()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Close the cursor now (rather than whenever ``__del__`` is
|
|
|
|
called). The cursor will be unusable from this point
|
|
|
|
forward; an exception will be raised if any operation is
|
|
|
|
attempted with the cursor.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``execute(operation [,params])``
|
2017-02-24 19:07:48 -05:00
|
|
|
Execute (prepare) a database operation (query or command).
|
|
|
|
Parameters may be provided (as a sequence
|
|
|
|
(e.g. tuple/list)) and will be bound to variables in the
|
|
|
|
operation. Variables are specified in a database-specific
|
|
|
|
notation that is based on the index in the parameter tuple
|
|
|
|
(position-based rather than name-based).
|
|
|
|
|
|
|
|
The parameters may also be specified as a sequence of
|
|
|
|
sequences (e.g. a list of tuples) to insert multiple rows
|
|
|
|
in a single operation.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Using SQL terminology, these are the possible result
|
|
|
|
values from the ``execute()`` method:
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
- If the statement is DDL (e.g. ``CREATE TABLE``), then 1 is
|
2017-02-24 19:07:48 -05:00
|
|
|
returned.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
- If the statement is DML (e.g. ``UPDATE`` or ``INSERT``), then the
|
2017-02-24 19:07:48 -05:00
|
|
|
number of rows affected is returned (0 or a positive
|
|
|
|
integer).
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
- If the statement is DQL (e.g. ``SELECT``), ``None`` is returned,
|
2017-02-24 19:07:48 -05:00
|
|
|
indicating that the statement is not really complete until
|
|
|
|
you use one of the 'fetch' methods.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``fetchone()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Fetch the next row of a query result, returning a single
|
|
|
|
tuple.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``fetchmany([size])``
|
2017-02-24 19:07:48 -05:00
|
|
|
Fetch the next set of rows of a query result, returning as
|
|
|
|
a list of tuples. An empty list is returned when no more
|
|
|
|
rows are available. The number of rows to fetch is
|
2017-04-05 12:14:26 -04:00
|
|
|
specified by the parameter. If it is ``None``, then the
|
2017-02-24 19:07:48 -05:00
|
|
|
cursor's arraysize determines the number of rows to be
|
|
|
|
fetched.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``fetchall()``
|
2017-02-24 19:07:48 -05:00
|
|
|
Fetch all rows of a query result, returning as a list of
|
|
|
|
tuples. Note that the cursor's arraysize attribute can
|
|
|
|
affect the performance of this operation.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``setinputsizes(sizes)``
|
2017-02-24 19:07:48 -05:00
|
|
|
(Note: this method is not well-defined yet.) This can be
|
|
|
|
used before a call to ``execute()`` to predefine memory
|
|
|
|
areas for the operation's parameters. sizes is specified
|
|
|
|
as a tuple -- 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
|
2017-04-05 12:14:26 -04:00
|
|
|
``None``, then no predefined memory area will be reserved
|
2017-02-24 19:07:48 -05:00
|
|
|
for that column (this is useful to avoid predefined areas
|
|
|
|
for large inputs).
|
|
|
|
|
|
|
|
This method would be used before the ``execute()`` method is
|
|
|
|
invoked.
|
|
|
|
|
|
|
|
Note that this method is optional and is merely provided
|
|
|
|
for higher performance database interaction.
|
|
|
|
Implementations are free to do nothing and users are free
|
|
|
|
to not use it.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``setoutputsize(size [,col])``
|
2017-02-24 19:07:48 -05:00
|
|
|
(Note: this method is not well-defined yet.)
|
|
|
|
|
|
|
|
Set a column buffer size for fetches of large columns
|
|
|
|
(e.g. LONG). The column is specified as an index into the
|
2017-04-05 12:14:26 -04:00
|
|
|
result tuple. Using a column of ``None`` will set the default
|
2017-02-24 19:07:48 -05:00
|
|
|
size for all large columns in the cursor.
|
|
|
|
|
|
|
|
This method would be used before the ``execute()`` method is
|
|
|
|
invoked.
|
|
|
|
|
|
|
|
Note that this method is optional and is merely provided
|
|
|
|
for higher performance database interaction.
|
|
|
|
Implementations are free to do nothing and users are free
|
|
|
|
to not use it.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
DBI Helper Objects
|
2017-02-24 19:07:48 -05:00
|
|
|
==================
|
|
|
|
|
|
|
|
Many databases need to have the input in a particular format for
|
|
|
|
binding to an operation's input parameters. For example, if an
|
2017-04-05 12:14:26 -04:00
|
|
|
input is destined for a ``DATE`` column, then it must be bound to the
|
2017-02-24 19:07:48 -05:00
|
|
|
database in a particular string format. Similar problems exist
|
2017-04-05 12:14:26 -04:00
|
|
|
for "Row ID" columns or large binary items (e.g. blobs or ``RAW``
|
2017-02-24 19:07:48 -05:00
|
|
|
columns). This presents problems for Python since the parameters
|
|
|
|
to the ``execute()`` method are untyped. When the database module
|
|
|
|
sees a Python string object, it doesn't know if it should be bound
|
2017-04-05 12:14:26 -04:00
|
|
|
as a simple CHAR column, as a raw binary item, or as a ``DATE``.
|
2017-02-24 19:07:48 -05:00
|
|
|
|
|
|
|
To overcome this problem, the 'dbi' module was created. This
|
|
|
|
module specifies some basic database interface types for working
|
|
|
|
with databases. There are two classes: 'dbiDate' and 'dbiRaw'.
|
|
|
|
These are simple container classes that wrap up a value. When
|
|
|
|
passed to the database modules, the module can then detect that
|
2017-04-05 12:14:26 -04:00
|
|
|
the input parameter is intended as a ``DATE`` or a ``RAW``. For symmetry,
|
|
|
|
the database modules will return ``DATE`` and ``RAW`` columns as instances
|
2017-02-24 19:07:48 -05:00
|
|
|
of these classes.
|
|
|
|
|
|
|
|
A Cursor Object's 'description' attribute returns information
|
2017-04-05 12:14:26 -04:00
|
|
|
about each of the result columns of a query. The 'type_code' is
|
|
|
|
defined to be one of five types exported by this module: ``STRING``,
|
|
|
|
``RAW``, ``NUMBER``, ``DATE``, or ``ROWID``.
|
2017-02-24 19:07:48 -05:00
|
|
|
|
|
|
|
The module exports the following names:
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``dbiDate(value)``
|
2017-02-24 19:07:48 -05:00
|
|
|
This function constructs a 'dbiDate' instance that holds a
|
|
|
|
date value. The value should be specified as an integer
|
|
|
|
number of seconds since the "epoch" (e.g. ``time.time()``).
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``dbiRaw(value)``
|
2017-02-24 19:07:48 -05:00
|
|
|
This function constructs a 'dbiRaw' instance that holds a
|
|
|
|
raw (binary) value. The value should be specified as a
|
|
|
|
Python string.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``STRING``
|
2017-02-24 19:07:48 -05:00
|
|
|
This object is used to describe columns in a database that
|
|
|
|
are string-based (e.g. CHAR).
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``RAW``
|
2017-02-24 19:07:48 -05:00
|
|
|
This object is used to describe (large) binary columns in
|
|
|
|
a database (e.g. LONG RAW, blobs).
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``NUMBER``
|
2017-02-24 19:07:48 -05:00
|
|
|
This object is used to describe numeric columns in a
|
|
|
|
database.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``DATE``
|
2017-02-24 19:07:48 -05:00
|
|
|
This object is used to describe date columns in a
|
|
|
|
database.
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
``ROWID``
|
2017-02-24 19:07:48 -05:00
|
|
|
This object is used to describe the "Row ID" column in a
|
|
|
|
database.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
Acknowledgements
|
2017-02-24 19:07:48 -05:00
|
|
|
================
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2017-02-24 19:07:48 -05:00
|
|
|
Many thanks go to Andrew Kuchling who converted the Python
|
|
|
|
Database API Specification 1.0 from the original HTML format into
|
2022-11-16 17:11:31 -05:00
|
|
|
the PEP format in 2001.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
2022-11-16 17:11:31 -05:00
|
|
|
Greg Stein is the original author of the Python Database API
|
|
|
|
Specification 1.0. Marc-André later continued maintenance of the API as
|
|
|
|
an editor.
|
2001-03-29 12:00:15 -05:00
|
|
|
|
|
|
|
Copyright
|
2017-02-24 19:07:48 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the Public Domain.
|