Python 官方文档:入门教程 => 点击学习
pandas告警UserWarning: pandas only supports sqlAlchemy connectable 一、报错信息二、老的书写方式三、新的书写方式 一、报错信息
使用老的书写方式从数据库导入数据到pandas, 会打出一条warning信息:
UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBapi2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
老的书写方式为:
import pyMysqlimport pandas as pddb_host = 'localhost'user = 'root'passwd = '123456'db = 'mytestdb'conn = pymysql.connect(host=db_host, user=user, passwd=passwd, db=db, charset='utf8')sql = 'SELECT * FROM students'pd.set_option('display.unicode.ambiguous_as_wide', True)pd.set_option('display.unicode.east_asian_width', True)df = pd.read_sql(sql, conn)print(df)conn.close()
按照提示,推荐使用SQLAlchemy,需要先安装SQLAlchemy库:
pip install sqlalchemy
新版本的pandas库中con参数使用sqlalchemy库创建的create_engine对象 。创建create_engine对象(格式类似于URL地址)
from sqlalchemy import create_engineimport pandas as pdMYSQL_HOST = 'localhost'MYSQL_PORT = '3306'MYSQL_USER = 'root'MYSQL_PASSWord = '123456'MYSQL_DB = 'mytestdb'engine = create_engine('mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8' % (MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DB))sql = 'SELECT * FROM students'df = pd.read_sql(sql, engine)pd.set_option('display.unicode.ambiguous_as_wide', True)pd.set_option('display.unicode.east_asian_width', True)print(df)
来源地址:https://blog.csdn.net/hubing_hust/article/details/128425415
--结束END--
本文标题: Pandas告警UserWarning: pandas only supports SQLAlchemy connectable
本文链接: https://lsjlt.com/news/394069.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0