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>
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!