简体   繁体   中英

After execute connection.commit(), why the others cursor.execute() not work correctly?

I was using PyMysql lib to add some lines to my database, I got a very long list of SQL, maybe more than 150000 rows. So I thought to execute commit my every 5000 times, the code is here:

import pymysql

sql_list = ["sql1", "sql2", "sql3", ...]  # Very long list, more than 150000 rows
conn = pymysql.connect(
    host="localhost",
    port=3306,
    user="user",
    password="abc-123",
    database="test",
    charset="utf8"
)
cursor = conn.cursor()

flag = 0  # flag, for the count
for sql in sql_list:
    cursor.execute(sql)
    flag += 1
    if flag > 5000:
        conn.commit()
        flag = 0

When I tried to run this script, I got some rows in my database, but not the full.
Then I have changed the code to this:

import pymysql

sql_list = ["sql1", "sql2", "sql3", ...]  # Very long list, more than 150000 rows
conn = pymysql.connect(
    host="localhost",
    port=3306,
    user="user",
    password="abc-123",
    database="test",
    charset="utf8"
)
cursor = conn.cursor()

# flag = 0  # flag, for the count
for sql in sql_list:
    cursor.execute(sql)
    # flag += 1
    # if flag > 5000:
    #     conn.commit()
    #     flag = 0
conn.commit()

It works correctly! Why? Is my consideration redundant?
Any suggestion will be appreciate.

ChowRex, you commit every 5000 rows. I guess that after the last commit there are additional sqls to execute and these are not commited. I think you need a final commit.

flag = 0  # flag, for the count
for sql in sql_list:
    cursor.execute(sql)
    flag += 1
    if flag > 5000:
        conn.commit()
        flag = 0
conn.commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM