这个网址的Python对Mysql的操作是比较全的:http://www.kitebird.com/articles/pydbapi.html
---------------------------一个完整的例子------------------------------
?? #! /usr/bin/python
?? # animal.py - create animal table and
?? # retrieve information from it
?? import sys
?? import MySQLdb
?? # connect to the MySQL server
?? try:
?????? conn = MySQLdb.connect (host = "localhost", user = "testuser", passwd = "testpass", db = "test")
?? except MySQLdb.Error, e:
?????? print "Error %d: %s" % (e.args[0], e.args[1])
?????? sys.exit (1)
?? # create the animal table and populate it
?? try:
?????? cursor = conn.cursor ()
?????? cursor.execute ("DROP TABLE IF EXISTS animal")
?????? cursor.execute ("""CREATE TABLE animal (?name CHAR(40), category CHAR(40))""")
?????? cursor.execute ("""INSERT INTO animal (name, category)
?????????????? VALUES ('snake', 'reptile'), ('frog', 'amphibian'),?('tuna', 'fish'),? ('racoon', 'mammal')""")
?????? print "%d rows were inserted" % cursor.rowcount
?? # perform a fetch loop using fetchone()
?????? cursor.execute ("SELECT name, category FROM animal")
?????? while (1):
?????????? row = cursor.fetchone ()
?????????? if row == None:
?????????????? break
?????????? print "%s, %s" % (row[0], row[1])
?????? print "%d rows were returned" % cursor.rowcount
?? # perform a fetch loop using fetchall()
?????? cursor.execute ("SELECT name, category FROM animal")
?????? rows = cursor.fetchall ()
?????? for row in rows:
?????????? print "%s, %s" % (row[0], row[1])
?????? print "%d rows were returned" % cursor.rowcount
?? # issue a query that includes data values literally in
?? # the query string, then do same thing using placeholders
?????? cursor.execute ("""UPDATE animal SET name = 'turtle' WHERE name = 'snake'""")
?????? print "%d rows were updated" % cursor.rowcount
?????? cur_name = "snake"
?????? new_name = "turtle"
?????? cursor.execute ("""UPDATE animal SET name = %s WHERE name = %s""", (new_name, cur_name))
?????? print "%d rows were updated" % cursor.rowcount
?? # create a dictionary cursor so that column values
?? # can be accessed by name rather than by position
?????? cursor.close ()
?????? cursor = conn.cursor (MySQLdb.cursors.DictCursor)
?????? cursor.execute ("SELECT name, category FROM animal")
?????? result_set = cursor.fetchall ()
?????? for row in result_set:
?????????? print "%s, %s" % (row["name"], row["category"])
?????? print "%d rows were returned" % cursor.rowcount
?????? cursor.close ()
?? except MySQLdb.Error, e:
?????? print "Error %d: %s" % (e.args[0], e.args[1])
?????? sys.exit (1)
?? conn.close ()
?? sys.exit (0)