Table of Contents generated with DocToc
- 17.1
Relational Database APO Specifications - 17.2
sqlite3 - 17.3
DBM-Style Database Modules - 17.4
shelve - Navigation
For accessing relational databases, the Python community has developed a standard known as the Python Database API Specification V2.0, or PEP 249 for short (the formal description can be found at )
At a high level, the database API defines a set of functions and objects for connecting to a database server, executing SQL queries, and obtaining results. Two primary objects are used for this:
- a
Connectionobject that manages the connection to the database and - a
Cursorobject that is used to perform queries.
To connect to a database, every database module provides a module-level function connect(parameters). The exact parameters depend on the database but typically include information such as the
- data source name,
- user name,
- password,
- host name, and
- database name.
In order to perform any operations on the database, you first create a connection c and then you call c.cursor() method to create a Cursor object.
cur.execute(query [, parameters])Executes a query or command query on the database.cur.fetchmany([size])Returns a sequence of result rows (e.g., a list of tuples)
you should never manually form queries using Python string operations like cur.execute("select * from table where name = "%s" % name"). If you do, it opens up your code to a potential SQL injection attack.
All database modules provide their own mechanism for value substitution.
When working with database data, built-in types such as integers and strings are usually mapped to an equivalent type in the database. However, for dates, binary data, and other special types, data management is more tricky.
The sqlite3 module provides a Python interface to the SQLite database library.
- The
dbmmodule is used to read standard UNIX-dbm database files. The gdbm module is used to read GNU dbm database files. - The
dbhashmodule is used to read database files created by the Berkeley DB library. - The
dumbdbmmodule is a pure-Python module that implements a simple DBM-style database on its own.
All of these modules provide an object that implements a persistent string-based dictionary. That is, it works like a Python dictionary except that all keys and values are restricted to strings. A database file is typically opened using a variation of the open() function.
The shelve module provides support for persistent objects using a special “shelf” object.
This object behaves like a dictionary except that all the objects it contains are stored on disk using a hash-table based database such as dbhash, dbm or gdbm. Unlike those modules, however, the values stored in a shelf are not restricted to strings. Instead, any object that is compatible with the pickle module may be stored. A shelf is created using the shelve.open() function.