代码中连接的sql表为"20230411_hangzhuanlie",表结构如下:
代码如下(按需修改用户名、库名等即可):
import pymysql
import pandas as pd
from sqlalchemy import create_enginehost = '127.0.0.1:3306/'
user_name = 'root'
password = '123456'
database_name = 'test'
table_name = '20230411_hangzhuanlie'db_data = 'mysql+pymysql://'+user_name+':'+password+'@'+host+database_name \+'?charset=utf8mb4'# 创建链接
engine = create_engine(db_data)df = pd.DataFrame({'id':[1,3],'dt':[2,3],'c1':[1,'sad'],'c2':[12,412],'c3':[123,412]})
df.to_sql(table_name, engine, if_exists='append', index=False)# 关闭连接
engine.dispose()