Skip to content Skip to sidebar Skip to footer

Mysql.connector Do Not Give The Last Database State In Python

I use mysql.connector library in Python to send query to database. But, when the database is changed after the initialization, the mysql.connector’s tools answer like if the data

Solution 1:

You will need to use a socket or if the changes occur frequently have your code re-run every x minutes

Solution 2:

I just need to .connect()maindb object and .close() it before each new need.

maindb.connect()

cursor.execute("SELECT `id`, `name` FROM `students` WHERE `id` = 0")
result = cursor.fetchall()
print(result)

maindb.close()

Solution 3:

The database maintains data integrity by preventing in-progress transactions from seeing changes made by other transactions (see transaction isolation levels).

You can commit your connection to allow it to see new changes:


cursor = maindb.cursor()


# Here, I will send outside the python script a MySQL query to modify the name of the student from “foo” to “bar” like this:# `UPDATE `students` SET `name` = 'bar' WHERE `students`.`id` = 0;`# Doesn't show the update
cursor.execute("SELECT `id`, `name` FROM `students` WHERE `id` = 0")
result = cursor.fetchall()
print(result)  

# Shows the update because we have committed.
maindb.commit()
cursor.execute("SELECT `id`, `name` FROM `students` WHERE `id` = 0")
result = cursor.fetchall()
print(result)

Post a Comment for "Mysql.connector Do Not Give The Last Database State In Python"