且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Python重复执行MySQL查询

更新时间:2023-01-29 21:51:48

您需要在每次查询后提交连接.这将提交当前事务,并确保下一个(隐式)事务将接管在前一个事务处于活动状态时所做的更改.

You need to commit the connection after each query. This commits the current transaction and ensures that the next (implicit) transaction will pick up changes made while the previous transaction was active.

# Main loop
while True:

    # SQL query
    sql = "SELECT * FROM table"

    # Read the database, store as a dictionary
    mycursor = mydb.cursor(dictionary=True)
    mycursor.execute(sql)

    # Store data in rows
    myresult = mycursor.fetchall()

    # Transfer data into list
    for row in myresult:
        myList[int(row["rowID"])] = (row["a"], row["b"], row["c"])

        print(myList[int(row["rowID"])])

    # Commit !
    mydb.commit()
    print("---")
    sleep (0.1)

这里的概念是隔离级别.从 docs (重点是我的):

The concept here is isolation levels. From the docs (emphasis mine):

可重复读取

这是InnoDB的默认隔离级别.同一事务中的一致读取读取由第一次读取建立的快照.

This is the default isolation level for InnoDB. Consistent reads within the same transaction read the snapshot established by the first read.