gpt4 book ai didi

python - 如何引入开关断路器以每 50 行执行一次插入

转载 作者:行者123 更新时间:2023-11-28 23:39:57 24 4
gpt4 key购买 nike

我当前的代码基本上是在遍历我的 excel 文件中的所有行之后进行批量插入。我想介绍一个开关断路器,它将每 50 行执行一次插入。

db = Database(settings)
elt_insert_line = "INSERT INTO elt_data VALUES"
for row in r:
elt_insert_line = elt_insert_line + "(" + row[2] + ", " + row[3] + "),"
db.execute(elt_insert_line.rstrip(",")).commit().cleanup()

最佳答案

使用模运算符和 IF 条件

不熟悉 python,但我认为你需要这样的东西

db = Database(settings)
elt_insert_line = "INSERT INTO elt_data VALUES"
for row in r:
elt_insert_line = elt_insert_line + "(" + row[2] + ", " + row[3] + "),"

if r % 50 = 0 then
(
db.execute(elt_insert_line.rstrip(",")).commit().cleanup()
elt_insert_line = "INSERT INTO elt_data VALUES"
)

--one aditional at the end of the for
db.execute(elt_insert_line.rstrip(",")).commit().cleanup()

关于python - 如何引入开关断路器以每 50 行执行一次插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com