使用python从db获取最后插入的值

k5hmc34c  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(223)

我有一个每分钟插入一个新值的数据库,我想每分钟连续获取最新插入的值

cursor = connection.cursor () 
cursor.execute ("select time from gbp_inr_min order by id desc limit 1")
while True:
       row = cursor.fetchone
       print (row)

       time.sleep(60)
connection.close ()
sys.exit()

每当我运行这段代码时,它都会在一开始就给出正确的输出,之后它就会显示出来 NONE

iovurdzv

iovurdzv1#

您需要运行查询

cursor.execute ("select time from gbp_inr_min order by id desc limit 1")

每次在循环中,因为在这一行之后不再执行查询,所以游标只能获取在执行时返回的行(因为您的查询 LIMIT 1 ,因此返回一行就可以了)。

相关问题