Python连接Mysql数据库

来源:互联网 发布:mysql deleted表 编辑:程序博客网 时间:2024/05/16 12:03
# -*- coding: UTF-8 -*-


import MySQLdb


# 打开数据库连接
db = MySQLdb.connect("localhost","root","root","test" )


# 使用cursor()方法获取操作游标 
cursor = db.cursor()


# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")


# 使用 fetchone() 方法获取一条数据库。
data = cursor.fetchone()


print "Database version : %s " % data


# 关闭数据库连接
db.close()