from sqlalchemy import create_engine import pandas as pd # 创建MYSQL数据库连接 engine = create_engine('mysql://root:123456@192.168.0.21/testdb') # 读取电子表格xlsx文件 df = pd.read_excel('testDataImport.xlsx') # 导入数据到mysql数据库 df.to_sql(name='t2', con=engine, index=False, if_exists='replace')
mysql的连接字符串:
mysql://用户:密码@192.168.0.21/testdb
安装依赖项目
pip install sqlalchemy
pip install pandas
pip install mysqlclient
Collecting mysqlclient
Downloading mysqlclient-2.1.1-cp310-cp310-win_amd64.whl (178 kB)
|████████████████████████████████| 178 kB 15 kB/s
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.1
WARNING: You are using pip version 21.1.2; however, version 22.2.1 is available.
pip install openpyxl
Collecting openpyxl
Downloading openpyxl-3.0.10-py2.py3-none-any.whl (242 kB)
|████████████████████████████████| 242 kB 94 kB/s
Collecting et-xmlfile
Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.0.10
WARNING: You are using pip version 21.1.2; however, version 22.2.1 is available