Home > Database > Mysql Tutorial > body text

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

Susan Sarandon
Release: 2024-11-06 07:56:03
Original
820 people have browsed it

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:

<code class="python"># 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)</code>
Copy after login

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.

The above is the detailed content of Why Do Subsequent Queries in a Python Loop Return the Same Data from a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!