useful extension. Although we could generalize to support more options, at some point
we may need to revert to typing SQL commands in a client—part of the reason SQL is
a language is because it must support so much generality. Further extensions to these
scripts are left as exercises. Change this code as you like; it’s Python, after all.
SQL Resources
Although the examples we’ve seen in this section are simple, their techniques scale up
to much more realistic databases and contexts. The websites we studied in the prior
part of the book, for instance, can make use of SQL-based systems such as MySQL to
store page state information as well as long-lived client information. Because MySQL
(among others) supports both large databases and concurrent updates, it’s a natural
for website implementation.
There is more to database interfaces than we’ve seen, but additional API documentation
is readily available on the Web. To find the full database API specification, search the
Web for “Python Database API.” You’ll find the formal API definition—really just a
text file describing the PEP (the Python Enhancement Proposal) under which the API
was hashed out.
Perhaps the best resource for additional information about database extensions today
is the home page of the Python database SIG. Go to http://www.python.org, click on
the Community and SIGs links there, and navigate to the database group’s page, or run
a search. There, you’ll find API documentation (this is where it is officially maintained),
links to database vendor–specific extension modules, and more. And as always, see the
PyPI website and search the Web at large for related third-party tools and extensions.
ORMs: Object Relational Mappers
In this chapter, we’ve seen OODBs that store native Python objects persistently, as well
as SQL databases that store information in tables. It turns out that there is another class
of system that attempts to bridge the object and table worlds, which I’ve hinted at earlier
in this chapter: ORMs graft the Python class model onto the tables of relational data-
bases. They combine the power of relational database systems with the simplicity of
Python class-based syntax—you don’t need to forgo SQL-based databases, but you can
still store data that seems like Python objects to your scripts.
Today, there are two leading open source third-party systems that implement this
mapping: SQLObject and SQLAlchemy. Both are fairly complex systems that we can-
not do full justice to in this text, and you’re best off researching their documentation
on the Web for the full story (there are also dedicated books covering SQLAlchemy
today). Moreover, neither is completely Python 3.X ready as I write these words, so we
can’t run live examples with them in this text.
1354 | Chapter 17: Databases and Persistence