Python使用pymysql从MySQL数据库中读出数据的方法

前端之家收集整理的这篇文章主要介绍了Python使用pymysql从MySQL数据库中读出数据的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

python3.x已经不支持MysqLdb了,支持的是pyMysqL

使用pandas读取MysqL数据时,使用sqlalchemy,出现No module named ‘MysqLdb'错误

安装:打开Windows PowerShell,输入pip3 install PyMysqL即可

import pyMysqL.cursors
import pyMysqL
import pandas as pd

#连接配置信息
config = {
   'host':'127.0.0.1','port':3306,#MysqL默认端口
   'user':'root',#MysqL默认用户名
   'password':'1234','db':'house',#数据库
   'charset':'utf8mb4','cursorclass':pyMysqL.cursors.DictCursor,}

# 创建连接
con= pyMysqL.connect(**config)
# 执行sql语句
try:
 with con.cursor() as cursor:
  sql="select * from community_view"
  cursor.execute(sql)
  result=cursor.fetchall() 
finally:
 con.close();
df=pd.DataFrame(result)#转换成DataFrame格式
df.head()

以上这篇Python使用pyMysqLMysqL数据库中读出数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

猜你在找的Python相关文章