Entry
How can I access MySQL from Python?
Should I use MySQLmodule or MySQLdb?
Mar 2nd, 2000 16:34
Nathan Wallace, unknown unknown, Andy Dustman
This was written by Andy Dustman.
Well, I'm the author of MySQLdb. MySQLdb is designed to work well in a
threaded environment (gives up the interpreter lock during blocking
calls), and to work with MySQL-3.22 and up; I have not tested it with
3.23, but once it stabilizes a bit, I will ensure that it continues to
work. Architecturally, it's really two modules:
_mysql: low-level C interface, transliteration of the MySQL C API to an
object-oriented Python interface.
MySQLdb: Python wrapper to _mysql that provides a DB API v2.0-compatible
interface.
One weird/interesting thing it does is it lets you specify how values
are converted going from MySQL to Python and vice versa. There are two
dictionaries that key on types, which map to functions which do the
conversion, with sensible default conversions. (MySQL actually returns
all values to the C API as strings, and all values passed to MySQL must
be part of a literal query string.)
0.1.2 is the current version. I have reports that it dumps core on
RedHat/Alpha platforms, but I can't reproduce it on RedHat/Intel;
patches welcome. 0.1.1 doesn't have this problem, and it's still
available in the same location (no direct link on the page, however).
Other people have reported getting it to work on that crackpot OS from
out of Redmond. It also includes a patch to make Zope's ZMySQLDA to work
with it instead of MySQLmodule. Also check this page:
http://www.zope.org/Members/adustman/MySQLdb
I haven't looked at MySQLmodule a whole lot lately. Here's what I THINK
I know about it, some of which may be wrong:
- Doesn't give up the interpreter lock during blocking calls, causing
threads to block (bad for threaded things like Zope).
- MySQL-3.21 compatible only.
- Also a split interface (C module + Python wrapper).
- The C module exports a Perl-style interface (ick).
- Not really actively supported. Joe Skinner (original MySQLmodule
author) has expressed optimism that he might finally stop getting bugged
about MySQLmodule. :)