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

标签 python mysql python-3.x

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

相关文章:

python - mod_python Trac实例性能分析

python - TensorFlow - 导入元图并使用其中的变量

php 文件查询不起作用

mysql - 如何为下表数据和条件创建一行sql结果

python - 将值连接到列表中到 python 中的键

python - 在 python 中制作 JSON 对象

php - 没有递归的PHP中的树算法

python - 如何在python中使用\K作为正则表达式?

python-3.x - 当Python中的嵌套列表内满足值时,如何结束While循环?

python - redis py 和 hgetall - 为什么键值有一个 b""?