调试一下这段代码import pandas as pd import sqlalchemy from sqlalchemy import create_engine engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang') # 数据存入数据库 table_name = ['景点信息-分类后.xls','用户景点行为.xls','游记信息-分类后.xls','用户游记行为.xls','所有关系.xls'] file_name = ['scenery','scenery_action','artical','artical_action','relationship'] for i,j in zip(table_name,file_name): # df = pd.read_excel(i) # df.to_sql(j, engine,if_exists='replace') df = pd.read_excel(i, encoding='utf-8') df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4', dtype={'column_name': sqlalchemy.types.VARCHAR(length=255, collation='utf8mb4_unicode_ci')})
时间: 2023-06-25 22:04:30 浏览: 218
这段代码用于将多个 Excel 文件中的数据存入 MySQL 数据库中,并且指定表名。其中,循环中的 `zip(table_name,file_name)` 函数用于将名和表名一一对应。可以按照以下方式进行调试:
1. 确认数据库连接信息是否正确。可以尝试直接连接数据库,看是否能够成功连接,例如:
```
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/3chuang')
conn = engine.connect()
```
2. 确认需要读取的 Excel 文件路径是否正确。可以尝试使用 `os.path.exists()` 函数检查文件是否存在,例如:
```
import os
for i in table_name:
if not os.path.exists(i):
print(f"File {i} does not exist!")
```
3. 确认读取 Excel 文件时的编码是否正确。可以尝试使用 `pd.read_excel()` 函数时指定不同的编码,例如:
```
df = pd.read_excel(i, encoding='utf-8')
```
4. 确认存入数据库时的参数是否正确。可以尝试使用不同的参数,例如:
```
df.to_sql(j, engine, if_exists='replace', index=False, charset='utf8mb4', dtype={'column_name': sqlalchemy.types.String(length=255)})
```
通过以上方法进行调试,可以找到代码中的问题并解决。
阅读全文
相关推荐


















