PyMySQL obviously returns old / instant values ​​/ non-retry?

I use pymysql.cursors and a simplified code example that loads a row from a table and prints it every second:

 #!/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) 

A state is a single row table in the MariaDb database.

Now that this is done, if I start the MySQL client and modify the contents of the table, this script funly deflates the original value; that is, he apparently does not consult the database (!).

I am new to Python and I am definitely new to PyMySQL, so it states that if this is a stupid question, but I have RTM a bit, and it looks weird.

+5
source share
1 answer

I do not understand why this is necessary, but you can fix it with

  • Adding autocommit=True to connect() parameters.

  • Call conn.commit() after the cursor.execute() command.

It seems to be starting a transaction with a snapshot or something by default. I (nervously!) Posted an issue to the pymysql repository since I didn't hear anything here. It was immediately closed with an explanation

This is a repeatable read.

If someone knows something better than using autocommit , let me know.

+5
source

Source: https://habr.com/ru/post/1235370/


All Articles