【发布时间】:2023-01-26 06:44:35
【问题描述】:
我想将 df 插入到 mysql 表中,但我想比现在快得多,因为它非常慢。这是我的代码。
for i in range(len(mydf)):
try:
mydf.iloc[i:i+1].to_sql(save_table, con=my_engine, schema='my_schema', index=False, if_exists='append')
except exc.IntegrityError as e:
pass
except Exception as e:
logging.error("General exception was passed. Error was {}".format(e))
pass
我以前的方法是这样做的,但是如果表已经有该行,它将在重复的行上无限循环。因为它会开始使用这个过程。
try:
mydf.to_sql(save_table, con=engine, index=False, if_exists='append')
except exc.IntegrityError as e:
logging.info("Bypassing duplicates")
except Exception as e:
logging.info("General exception was passed. Error was {}".format(e))
想知道我是否应该将我的 df 转换为列表并以这种方式循环遍历它,或者有更好的方法吗?
【问题讨论】: