Database API explanation
skip at mojam.com
Fri Feb 18 13:04:21 EST 2000
Egbert> I need some help or explanation on the Python Database API
Egbert> Specification .0 especially how to use them with MySQL, but
Egbert> of course some general tutorial is very welcome as well.
Egbert> The specifications themselves are less than illuminating for me.
Egbert> Earlier I asked more or less the same question, but no reaction.
Egbert> But somebody must have written something, I suppose.
Having overcome the same hurdle with some help from the list, I'll toss out
a few things I learned.
To connect to a MySQLdb database use something like
db = MySQLdb.connect(host='localhost', user='root', passwd='my-passwd',
Once you have a connection object you can create a cursor and execute SQL
cursor = db.cursor()
cursor.execute("select start, city, state, venue from event "
"where event.name = %s and "
"event.city = %s", ("Brown,Greg", "San Jose"))
for item in cursor.fetchall():
Note the use of %s formatters in the SQL statement. This is based upon
MySQLdb's setting of its paramstyle attribute to "format". The DB api
indicates other alternatives, but I think you're constrained to a single
style based upon the needs of the underlying SQL implementation. The items
returned by the cursor.fetch* routines are lists whose elements are ordered
the same as the names in the select statement (e.g. "start, city, state,
That's about all I'm using of the interface at the moment. That should get
Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
"Languages that change by catering to the tastes of non-users tend not to do
so well." - Doug Landauer
More information about the Python-list