【问题标题】:To fetch last inserted value from db using python使用 python 从 db 中获取最后插入的值
【发布时间】:2018-10-01 10:47:51
【问题描述】:

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

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

【问题讨论】:

  • row 等于Cursor.fetchone 方法,我想应该调用它来获取像row = cursor.fetchone() 这样的行

标签: python mysql


【解决方案1】:

您需要运行查询

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

每次循环,因为在此行之后不再执行查询,因此,游标只能获取 在其执行时返回的行(因为您的查询已LIMIT 1,因此返回单行是可以预期的结果,仅此而已)。

【讨论】:

  • 我按照你说的做了while True: cursor.execute ("select time from gbp_inr_min order by id desc limit 1") row = cursor.fetchone print (row) 但它给出了一个错误<bound method Cursor.fetchone of <MySQLdb.cursors.Cursor object at 0x7ff438fede10>>
  • @VibhorKarnawat 仍然无法正常工作?更新:见 Azat Ibrakov 的评论,你需要括号
  • 您是否阅读了 Azat Ibrakovs 对该问题的评论? cursor.fetchone 只是对fetchone 方法的引用。您必须通过添加括号来调用它:cursor.fetchone()
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2010-09-19
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多