MySQLdb FAQ: Using MySQLdb

Next Previous Contents

## 3. Using MySQLdb

MySQLdb is a Python Database API Specification 2.0 database module, so you should be familiar with the spec. Deviations from the spec are documented in the MySQLdb documentation.

## 3.1 cursor.rollback() always fails!

MySQLdb now supports transactions if the server supports transaction-safe tables (TSTs) and you are using them. If your server doesn't support them, rollbacks will always fail, as they should, because it can't do what you asked. Even if your server does support them, rollbacks will fail if you modified any non-TST tables.

OTOH, `cursor.commit()`, which attempts to commit the transaction to the database, always succeeds, because MySQL essentially is always in auto-commit mode (unless you told it otherwise).

## 3.2 How do I use some of the special MySQL features?

First answer: Don't, if you can avoid it. Your program will not be portable to other databases.

Second answer: Nearly all the special API calls are implemented on the _mysql connection object, and the MySQLdb connection object can also invoke them. See the built-in module docs to find out what ones are implemented, and the MySQL C API docs to see what they do.

## 3.3 I still wanna use _mysql directly.

Well, it `may` be appropriate in some cirumstances. ZMySQLDA does this, because Zope's ZRDB module is an API in itself, and too many layers of APIs tend to make a mess of things. Besides, it was actually pretty easy to do it that way and it probably improves the performance a bit.

1. Read the MySQL docs, particularly the C API, for an overview.
2. Read the MySQLdb docs. This shows how the C API is transliterated into Python. Plus some examplesa are in there.
3. Read the MySQLdb sources, particularly MySQLdb/cursors.py. That one file contains most of the gory details, particularly in the execute and _query methods.

Next Previous Contents