An mysql-python tutorial?
Andy Dustman
farcepest at gmail.com
Sat Jan 29 10:42:35 EST 2005
It's a pretty good tutorial, thought I would recommend you forget about
the fetchone() example. The example below demonstrates three additional
features that will make your life easier: MySQL option files, tuple
unpacking, and cursors as iterators (fourth feature: the default host
is localhost; this is rapidly turning into the Spanish Inquisition
sketch):
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(db="db56a", read_default_file="~/.my.cnf")
cursor = db.cursor()
cursor.execute("SELECT name, species FROM animals")
for name, species in cursor:
print name, "-->", species
(I also shy away from doing SELECT *; what if your schema changes?)
(If the indentation is hosed, blame Google Groups.)
More information about the Python-list
mailing list