"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 Does My Python MySQL Query Keep Returning the Same Data?

Why Does My Python MySQL Query Keep Returning the Same Data?

Published on 2024-11-19
Browse:576

Why Does My Python MySQL Query Keep Returning the Same Data?

Queries on MySQL from Python Yielding Identical Data

Repeatedly querying a MySQL database from Python to retrieve dynamic data poses a challenge. It is known that merely iterating through a query in a loop does not suffice to fetch fresh data from the database.

Your provided code exemplifies this issue:

for i in range(listSize):
    #...
    mycursor = mydb.cursor(dictionary=True)
    mycursor.execute(sql)
    #...

Even with attempts using fetchall, fetchmany, and fetchone, the results remain stagnant.

The Solution: Committing the Connection

To resolve this, each query execution must be followed by a commit of the connection. This action concludes the ongoing transaction and initiates a new one, enabling the subsequent query to detect alterations made during the previous transaction.

while True:
    #...
    mycursor = mydb.cursor(dictionary=True)
    mycursor.execute(sql)
    #...
    mydb.commit()
    #...

Isolation Levels and the REPEATABLE READ Default

This concept is rooted in isolation levels. By default, MySQL employs REPEATABLE READ for InnoDB. This implies that within a transaction, subsequent reads will retain the snapshot established by the transaction's initial read. Hence, without committing, subsequent queries will not capture any alterations.

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