一、報(bào)錯(cuò)信息
使用老的書寫方式從數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)到pandas, 會(huì)打出一條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 pymysql
import pandas as pd
db_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參數(shù)使用sqlalchemy庫創(chuàng)建的create_engine對(duì)象 。創(chuàng)建create_engine對(duì)象(格式類似于URL地址)文章來源:http://www.zghlxwxcb.cn/news/detail-570811.html
from sqlalchemy import create_engine
import pandas as pd
MYSQL_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)
文章來源地址http://www.zghlxwxcb.cn/news/detail-570811.html
到了這里,關(guān)于Pandas告警UserWarning: pandas only supports SQLAlchemy connectable的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!