问题描述
我正在使用pymysql.cursors
和一个简化的代码示例,该示例从表中加载一行并每秒打印一次:
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)
状态是MariaDb数据库中只有一行的表.
state is a table with a single row in a MariaDb database.
现在,如果我启动一个MySQL客户端并更改表的内容,在运行此脚本时,此脚本会愉快地保持抽出原始值.也就是说,它显然没有查询数据库(!).
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(!).
我是Python的新手,我绝对是PyMySQL的新手,所以如果这是一个愚蠢的问题,那么请向Apols提问,但是我有 RTM 有点奇怪.
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.
推荐答案
我不明白为什么为什么,但是您可以通过以下任何一种方式修复它
I do not understand why this is necessary, but you can fix it by either
-
在
connect()
参数中添加autocommit=True
.
在cursor.execute()
命令之后调用conn.commit()
.
似乎它在快照或默认情况下启动事务.我(非常紧张!)在pymysql仓库上提交了 issue ,因为我没有听到什么都回到这里.随即关闭,并附有说明
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
如果有人比使用autocommit
更好的话,请告诉我.
If anyone knows something better than using autocommit
let me know.
这篇关于PyMySQL返回旧/快照值/不重新运行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!