MySQL/Python-提交的更改未出现在循环中 [英] MySQL/Python -- committed changes not appearing in loop

查看:112
本文介绍了MySQL/Python-提交的更改未出现在循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MySQL Connector/Python,我有一个循环,该循环每2秒检查一次更改值.没有所有的肉,这就是循环(打印在那里是出于测试目的:

Using MySQL Connector/Python I have a loop that keeps checking a value for a change every 2 seconds. Without all the meat, here is the loop (the print is there for testing purposes:

try:
   while True:
       request = database.get_row(table="states", wherecol="state", whereval="request_from_interface")[0]
       print(request.value)
       time.sleep(2)
except KeyboardInterrupt:
   pass
# back to normal operation

get_row是一个简单的选择查询,它返回namedtuple fetchall(因此,末尾的[0]只会是一行).

get_row is a simple select query and it returns the namedtuple fetchall (hence the [0] at the end, it will only ever be one row).

问题在于,一旦它获得了初始值,即使我更改它,它也会继续返回该值.例如,如果启动是"0",即使我去管理员更改它,即使我打开一个新的终端并更改并提交它,它也会一直打印"0".我尝试将query_cache_size设置为0,仍然没有运气.我认为这是一个提交问题,但事实并非如此.如果更改并重新连接,我会看到更改,但是我不确定为什么在程序中更改不会更改.有什么想法吗?

The problem is that once it gets the initial value, it keeps returning that value even if I change it. For example, if it is "0" to start, it keeps printing "0" even if I go to adminer and change it and even if I open a new terminal and change it and commit it. I've tried setting the query_cache_size to 0, still no luck. I thought it was a commit problem, but that wasn't it, either. If I change it and reconnect, I see the change, but I'm not sure why it won't change while in the program. Any thoughts?

如果出现这种情况,是的,我将关闭游标并在每次调用时都抓住一个新游标.

in case it comes up, yes I'm closing the cursor and grabbing a new cursor with every call.

推荐答案

默认

The default isolation level of InnoDB is REPEATABLE READ. This means that subsequent consistent SELECT statements will read the same snapshot established by the first within a transaction. Though you're opening and closing cursors, you're probably doing that inside a transaction and so you're reading the established snapshot over and over again. Either ROLLBACK or COMMIT to end the current transaction so that you can read values committed after the snapshot was taken.

这篇关于MySQL/Python-提交的更改未出现在循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆