Python:连接MySQL数据库
1.Pycharm交互界面下安装pymysql库pip install pymysql安装成功画面2.Pycharm下新建.py文件,编写如下代码import pymysql# 打开数据库连接db = pymysql.connect(host='localhost',user='tiger',password='R324324d599,',database='d1',port=3306,chars
·
1.Pycharm交互界面下安装pymysql库
pip install pymysql
安装成功画面
2.Pycharm下新建.py文件,编写如下代码
import pymysql
# 打开数据库连接
db = pymysql.connect(host='localhost',user='tiger',password='R324324d599,',database='d1',port=3306,charset='utf8')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print "Database version : %s " % data
# 关闭数据库连接
db.close()
3.右键运行该.py文件
4.参考
https://www.runoob.com/python/python-mysql.html
更多推荐
所有评论(0)