[hxlwh@localhost shell]$ cat mysql.py
# coding:utf-8
import sys
sys.path.append("/usr/lib64/python2.7/site-packages")
import MySQLdb
try:
# 连接数据库
db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="root", db="test")
# 连接成功,可以执行其他操作
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version : %s " % data)
except MySQLdb.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
finally:
# 关闭数据库连接
if db:
db.close()
[hxlwh@localhost shell]$
07-28
7585

12-25