游标不断查询数据库的数据,并用frechone方法得到数据,当数据库的数据存在更新时,游标查询的数据却没有变化:
import pymysql import time db = pymysql.connect(host='192.168.11.111','root','123abc','test') cursor = db.cursor() while True: sql = 'SELECT DATETIME FROM table01 ORDER BY idc DESC LIMIT 1;' cursor.execute(sql) print(cursor.fetchone()) time.sleep(10)
第一种处理方式,就是在游标查询后,提交一次事务:
import pymysql import time db = pymysql.connect(host='192.168.11.111','root','123abc','test') cursor = db.cursor() while True: sql = 'SELECT DATETIME FROM table01 ORDER BY idc DESC LIMIT 1;' cursor.execute(sql) print(cursor.fetchone())
#提交事务 db.commit() time.sleep(10)
第二就是每次都断开连接,即:db.close() ,但这样写,每次查询前都要进行登录,比较繁琐。