我有一小段查询 mySQL 的 python 代码,
while True:
print "running SQL query"
cursor.execute('select * from sites where stage="NEW"')
results = cursor.fetchall() #Fetch results
for row in results:
print "change detected" #simplified line for testing
print "sleeping"
sleep(10)
当没有记录匹配时,输出为:
running SQL query
sleeping
running SQL query
sleeping
手动创建匹配的 SQL 记录(允许循环重复的时间),输出仍然是:
running SQL query
sleeping
running SQL query
sleeping
停止脚本并重新启动,测试输出符合预期:
running SQL query
change detected
sleeping
那么结果被缓存了吗?或者查询没有重新运行?
我将如何处理这个问题,考虑到查询运行得非常频繁(我确实想知道关闭数据库连接并每次重新打开,但这似乎太过分了?)
如果你看文档 https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-commit.html for commit()
它可以提示您的代码为什么不起作用。
尝试使用database.commit()
after sleep(10)
看看它是否有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)