Python读取Excel内容,并追加到Mysql数据库表中

客户需求
使用Python一次性读取Excel文件中的内容,并一次性追加到数据库的表中

import pandas as pd
from sqlalchemy import create_engine
#获取需要插入数据库的数据
data = pd.read_excel(r"C:\Users\Administrator\Desktop\test.xlsx")
#获取数据库连接  user:用户名; password:密码; host:port 服务器地址:端口号; db:数据库名称
engine = create_engine("mysql+pymysql://{}:{}@{}/{}?charset=utf8".format('user','password','host:port','db'))
con = engine.connect()
#将数据插入数据库指定表中 注意:获取xlsx文件的字段名称和数据库表中的字段名称一致
#premoney:插入数据库中的表名字
data.to_sql(name='premoney', con=con, if_exists='append', index=False)
con.close()

你可能感兴趣的:(Python)