错误写法:
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