Python连接MySQL数据库

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import pymysql

# 打开数据库连接
db = MySQLdb.connect("host", "port", "user", "password","数据库名称", charset='utf8' )

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

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

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

print "Database version : %s " % data

# 关闭数据库连接
db.close()
posted @ 2019-04-17 11:10  测试一枚  阅读(533)  评论(0编辑  收藏  举报