1、首先确认本地使用的是Python2还是Python3,它们的mysql插件分别如下:
Python2 ---> MySQLdb
Python3 ---> PyMySQL
2、我本地是Python3故需要安装PyMySQL。进入File->Setting->Project Interpreter,点击“+”在弹框中进行“PyMySQL”模块的搜索并安装
3、 python代码
# -*- coding: UTF-8 -*- import pymysql.cursors # 打开数据库连接 connection = pymysql.connect (host='xx.xx.xx.xx', port=3306, user='xx', password='xx', db='xx',charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() SqlSelect = " select vw_stock_id,goods_id,quality from vw_stock limit 2" cursor.execute(SqlSelect ) result = cursor.fetchall() # 提交SQL connection.commit() print(result[0]) print(result[1]) # 关闭数据库连接 connection.close()
执行成功,获取数据库查询结果如下
标签:Python,数据库,connection,pymysql,cursor,xx,PyMySQL,连接 From: https://www.cnblogs.com/dabeen/p/17080064.html