我当前的代码基本上是在遍历我的 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/