This API has been defined to encourage similarity between thePython modules that are used to access databases. By doing this,we hope to achieve a consistency leading to more easily understoodmodules, code that is generally more portable across databases,and a broader reach of database connectivity from Python.
This interface specification consists of several items:
Comments and questions about this specification may be directed tothe 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.
The database interface modules should typically be named withsomething terminated bydb. Existing examples are:oracledb,informixdb, andpg95db. These modules should export severalnames:
modulename(connection_string)errorConnection Objects should respond to the following methods:
close()__del__ iscalled). The connection will be unusable from this pointforward; an exception will be raised if any operation isattempted with the connection.commit()rollback()cursor()callproc([params])These objects represent a database cursor, which is used to managethe context of a fetch operation.
Cursor Objects should respond to the following methods andattributes:
arraysizefetchmany(). This value is also usedwhen inserting multiple rows at a time (passing atuple/list of tuples/lists as the params value toexecute()). This attribute will default to a single row.Note that the arraysize is optional and is merely providedfor higher performance database interactions.Implementations should observe it with respect to thefetchmany() method, but are free to interact with thedatabase a single row at a time.
descriptionNonefor operations that do not return rows or if the cursorhas not had an operation invoked via theexecute() methodyet.The ‘type_code’ is one of the ‘dbi’ values specified inthe section below.
Note: this is a bit in flux. Generally, the first twoitems of the 7-tuple will always be present; the othersmay be database specific.
close()__del__ iscalled). The cursor will be unusable from this pointforward; an exception will be raised if any operation isattempted with the cursor.execute(operation[,params])The parameters may also be specified as a sequence ofsequences (e.g. a list of tuples) to insert multiple rowsin a single operation.
A reference to the operation will be retained by thecursor. If the same operation object is passed in again,then the cursor can optimize its behavior. This is mosteffective 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 isbest to use thesetinputsizes() method to specify theparameter types and sizes ahead of time. It is legal fora parameter to not match the predefined information; theimplementation should compensate, possibly with a loss ofefficiency.
Using SQL terminology, these are the possible resultvalues from theexecute() method:
CREATETABLE), then 1 isreturned.UPDATE orINSERT), then thenumber of rows affected is returned (0 or a positiveinteger).SELECT),None is returned,indicating that the statement is not really complete untilyou use one of the ‘fetch’ methods.fetchone()fetchmany([size])None, then thecursor’s arraysize determines the number of rows to befetched.Note there are performance considerations involved withthe size parameter. For optimal performance, it isusually best to use the arraysize attribute. If the sizeparameter is used, then it is best for it to retain thesame value from onefetchmany() call to the next.
fetchall()setinputsizes(sizes)execute() to predefine memoryareas for the operation’s parameters. sizes is specifiedas a tuple – one item for each input parameter. The itemshould be a Type object that corresponds to the input thatwill be used, or it should be an integer specifying themaximum length of a string parameter. If the item isNone, then no predefined memory area will be reservedfor that column (this is useful to avoid predefined areasfor large inputs).This method would be used before theexecute() method isinvoked.
Note that this method is optional and is merely providedfor higher performance database interaction.Implementations are free to do nothing and users are freeto not use it.
setoutputsize(size[,col])Set a column buffer size for fetches of large columns(e.g. LONG). The column is specified as an index into theresult tuple. Using a column ofNone will set the defaultsize for all large columns in the cursor.
This method would be used before theexecute() method isinvoked.
Note that this method is optional and is merely providedfor higher performance database interaction.Implementations are free to do nothing and users are freeto not use it.
Many databases need to have the input in a particular format forbinding to an operation’s input parameters. For example, if aninput is destined for aDATE column, then it must be bound to thedatabase in a particular string format. Similar problems existfor “Row ID” columns or large binary items (e.g. blobs orRAWcolumns). This presents problems for Python since the parametersto theexecute() method are untyped. When the database modulesees a Python string object, it doesn’t know if it should be boundas a simple CHAR column, as a raw binary item, or as aDATE.
To overcome this problem, the ‘dbi’ module was created. Thismodule specifies some basic database interface types for workingwith databases. There are two classes: ‘dbiDate’ and ‘dbiRaw’.These are simple container classes that wrap up a value. Whenpassed to the database modules, the module can then detect thatthe input parameter is intended as aDATE or aRAW. For symmetry,the database modules will returnDATE andRAW columns as instancesof these classes.
A Cursor Object’s ‘description’ attribute returns informationabout each of the result columns of a query. The ‘type_code’ isdefined to be one of five types exported by this module:STRING,RAW,NUMBER,DATE, orROWID.
The module exports the following names:
dbiDate(value)time.time()).dbiRaw(value)STRINGRAWNUMBERDATEROWIDMany thanks go to Andrew Kuchling who converted the PythonDatabase API Specification 1.0 from the original HTML format intothe PEP format in 2001.
Greg Stein is the original author of the Python Database APISpecification 1.0. Marc-André later continued maintenance of the API asan editor.
This document has been placed in the Public Domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0248.rst
Last modified:2025-02-01 08:55:40 GMT