查看是否安装、版本 pip show pymysql
1、安装 pip install pymysql
2、查询:
import pymysql # 连接本地mysql执行sql语句 def ExecuteLocalMysqlSelect(sql): conn = pymysql.connect(host='127.0.0.1' ,user='root' ,password='root' ,port=3306 ,db='analyst' ,charset='utf8') cur = conn.cursor() try: a = cur.execute(sql) except: conn.rollback() print('perform failed') finally: conn.commit() print('perform success') conn.close() #cur.description 获取列名 return a,cur.description,cur.fetchall()
注:
description 获取列名
fetchall 返回全部数据
fetchone 返回一行数据
fetchmany(size) 取出一定数量