UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string
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
conn = pymysql.connect(
host='localhost',
user='root',
passwd='guojiabao',
db='demo',
port=3306,
charset='utf8')
df = pd.read_sql('select * from hotsearch',conn)
df
正确写法:

from sqlalchemy import create_engine
import pandas as pd
HOST = 'localhost'
PORT = '3306'
USER = 'root'
PASSWORD = '123456'
DB = '数据库名'
engine = create_engine('mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8'
% (USER, PASSWORD, HOST, PORT, DB))
sql = 'SELECT * FROM 表名'
df = pd.read_sql(sql, engine)
df
更多推荐



所有评论(0)