Python 3连接数据库

阅读 75

2022-08-04


(一)安装pymysql

pip install

(二)查看版本号

import

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

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

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

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

print ("Database version : %s " % data)

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



精彩评论(0)

0 0 举报