Python—a few lines of code to store Excel directly to MySQL

Requirement: Import the Excel file into the database for storage.
Note: All symbols in the Excel table are all in English!!!

Demo file:
Insert picture description hereImplementation code:

import pymysql
import pandas as pd
from sqlalchemy import create_engine

file = r'./data.xlsx'
df = pd.read_excel(file)
engine = create_engine("mysql+pymysql://root:root@localhost:3306/python",encoding='utf-8')

df.to_sql('testexcel',con=engine,if_exists='replace',index=False)

Running result:
Insert picture description here
Reference: Save Excel into database

Guess you like

Origin blog.csdn.net/weixin_45666249/article/details/114317951