1. simple example [python] #-*-coding: UTF-8-*-import MySQLdb db = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'root') # If the connection is successful, a connection object cur = db is returned. cursor () # create a cursor to execute the SQL statement cur.exe cute ('select version () ') # execute the SQL statement row = cur. fetchone () # obtain a result tuples print 'server version: ', row [0] cur. close () db. close () 2. example 2 [python] #-*-coding: UTF-8-*-import MySQLdb import sys db = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'root', db = 'chen') # If the connection is successful, a connection object cursor = db is returned. cursor () cursor.exe cute ('drop table if exists animal ') cursor.exe cute ('create table animal (name char (40), category char (40 ))') cursor.exe cute ("insert into animal (name, category) values ('snake ', 'reptle'), ('frog', 'amphibian ')") cursor.exe cute ("SELECT name, category FROM animal") while (1): www.2cto.com row = cursor. fetchone () if row = None: break print "% s, % s" % (row [0], row [1]) print "Number of rows returned: % d "% cursor. rowcount db. commit () db. close ()