I'm using pymysql.cursors
and a simplified code example that loads a row from a table and prints it every second is:
#!/usr/bin/env python3
import pymysql.cursors
import time
conn = pymysql.connect(host='localhost',
# credentials etc.
cursorclass=pymysql.cursors.DictCursor)
while True:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM state limit 1;")
vals = cursor.fetchone()
print (vals)
time.sleep(1)
state is a table with a single row in a MariaDb database.
Now while this is running if I fire up a MySQL client and change the table's contents, this script merrily keeps pumping out the original value; i.e. it's apparently not consulting the database(!).
I'm newish to Python and I'm definitely new to PyMySQL, so apols if this is a daft question but I have RTM a bit and it just looks odd.
PyMySQL fetchAll The fetchAll method retrieves all (remaining) rows of a query result, returning them as a sequence of sequences. In the example, we retrieve all cities from the database table. This SQL statement selects all data from the cities table.
class pymysql.cursors. DictCursor (connection) A cursor which returns results as a dictionary. class pymysql.cursors.
PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb.
I do not understand why this is necessary, but you can fix it by either
Adding autocommit=True
into the connect()
parameters.
Calling conn.commit()
after the cursor.execute()
command.
Seems it starts a transaction at a snapshot or something by default. I (nervously!) submitted an issue on the pymysql repo, as I'd not heard anything back here. This was closed immediately with the explanation
It's repeatable read
If anyone knows something better than using autocommit
let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With