Home > Database > Mysql Tutorial > body text

Why Are My MySQL Queries from Python Returning Inconsistent Results?

Mary-Kate Olsen
Release: 2024-11-07 08:28:03
Original
570 people have browsed it

Why Are My MySQL Queries from Python Returning Inconsistent Results?

MySQL Queries from Python Yielding Inconsistencies

Your query is likely not retrieving the most up-to-date data because you're not committing changes to the database. By default, MySQL sets the isolation level to "REPEATABLE READ," which means subsequent queries within the same transaction view the initial snapshot of data rather than any changes made during the transaction.

To ensure that your data is up-to-date, you need to commit the connection after each query. This completes the current transaction and prepares the next transaction to fetch the most recent changes from the database.

Here's an updated version of your code with the necessary commit:

<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 changes!
    mydb.commit()
    print("---")
    sleep(0.1)</code>
Copy after login

With this modification, your code will now retrieve the latest data from the database during each query loop.

The above is the detailed content of Why Are My MySQL Queries from Python Returning Inconsistent Results?. 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!