使用python从数据库中获取最后插入的值
我有一个数据库,其中每分钟插入一个新值,我希望每分钟连续获取最新插入的值使用python从数据库中获取最后插入的值,python,mysql,Python,Mysql,我有一个数据库,其中每分钟插入一个新值,我希望每分钟连续获取最新插入的值 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.ex
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()
每当我运行这段代码时,它都会在一开始就给出正确的输出,之后它只会显示“无”您需要运行查询
cursor.execute ("select time from gbp_inr_min order by id desc limit 1")
在循环中的每一次,由于此行之后不再执行查询,因此,游标只能获取在执行时返回的行,因为您的查询限制为1,因此返回一行而不返回任何内容是可以预期的结果。row等于cursor.fetchone方法,我想应该调用它来获取一行,如row=cursor.fetchone我按照您说的做了,而True:cursor.execute select time from gbp_inr_min order by id desc limit 1 row=cursor.fetchone print row,但它给出了一个错误@VibhorKarnawat,仍然不起作用?Upd:请看Azat Ibrakov的评论,您需要括号。您是否阅读了Azat Ibrakov对问题的评论?cursor.fetchone只是对fetchone方法的引用。您必须通过添加括号来调用它:cursor.fetchone。