Python 读取excel写入数据库

表格数据如下:

Python 读取excel写入数据库_第1张图片

代码如下:

import pandas as pd

from sqlalchemy import create_engine

excelFile = r'D:\untitled\chatPy (version 1).xlsb.xlsx'
df = pd.DataFrame(pd.read_excel(excelFile))


engine =create_engine('mysql+pymysql://wang:123456@localhost:3306/vrapp1',encoding='utf8')
df.to_sql('attribute',con=engine,if_exists='replace',index=False)

注释:
①、r’D:\untitled\chatPy (version 1).xlsb.xlsx’ 文件路径及文件名。
②、‘mysql+pymysql://wang:123456@localhost:3306/vrapp1’
A、 wang代表的是数据库用户名
B、123456数据库密码;
C、vrapp1表示数据库名字
③、df.to_sql(‘attribute’,con=engine,if_exists=‘replace’,index=False)
A、attribute:表示数据库表名;
B、con=后面接前面一句的engine对象,
C、if_exists=‘replace’:表示如果已经数据表存在就进行替换。

结果:

Python 读取excel写入数据库_第2张图片

你可能感兴趣的:(Python)