"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Do Subsequent Queries in a Python Loop Return the Same Data from a MySQL Database?

Why Do Subsequent Queries in a Python Loop Return the Same Data from a MySQL Database?

Published on 2024-11-10
Browse:322

Why Do Subsequent Queries in a Python Loop Return the Same Data from a MySQL Database?

Retrieving Dynamic Data from MySQL Using Python Queries

When periodically querying a rapidly changing MySQL database from Python, you might expect a loop-based approach to consistently fetch the latest data. However, you may encounter a situation where the same data is repeatedly returned.

This issue stems from MySQL's default isolation level, REPEATABLE READ. With this level, reads within a transaction use a snapshot of the data as it existed at the start of the transaction. Subsequent queries within the same transaction will read from this snapshot rather than updating it.

To resolve this, you need to commit the connection after each query, ensuring that the next transaction will retrieve the latest data. Here's how you can modify your Python code:

# 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)

By adding mydb.commit() after each query, you force MySQL to commit the transaction and update the snapshot. This allows subsequent queries to access the freshest data.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3