数据库基本操作 [MySQLdb]


#!/usr/local/bin/python
import MySQLdb
connection = None
try:
    connection = MySQLdb.connect(host='localhost',user='lvdbing',passwd='123456',db='test')
    cursor = connection.cursor()
    cursor.execute("select * from test_book")
    for row in cursor.fetchall():
        print "id: ", row[0]
        print "bookname: ", row[1]
    connection.close()
except:
    if connection:
        connection.close()
    print "Connect database error!"