【问题标题】:I can't see continuously the last insert data from my MySQL database to my html page我无法连续看到从 MySQL 数据库到 html 页面的最后插入数据
【发布时间】:2021-04-29 01:25:58
【问题描述】:

我使用烧瓶框架,并使用此代码获得最后插入数据,但我想连续显示最后插入数据。当我重新启动烧瓶框架时,我得到了它。有什么帮助吗?

app.py

@app.route("/sensor" , methods=['POST','GET'])
def sensor():
   
   cursor = db.cursor()

   cursor.execute("SELECT Id,temperature,humidity FROM Bseonsor WHERE Id=(SELECT MAX(Id) FROM Bseonsor)")

   results = cursor.fetchall()
   
   return render_template('sensor.html',results=results)

sensor.html

<div>
    <table border="1" cellpadding="5" cellspacing="5">
    {% for row in results %}
        <tr> {{ row[1] }} {{ row[2] }} </tr>
    {% endfor %}
    </table>
</div>

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。您可以编辑您的问题以使用请求的信息更新它们。我从您的评论中为您提取了问题的代码。请务必使用tour 并阅读How to Ask 以开始使用此社区。快乐编码:)

标签: python html mysql flask last-insert-id


【解决方案1】:

我相信在对数据库进行选择时,您应该使用buffered=True 参数。

关于选择的注意事项:同样在上面的程序中,指定大小为 2 以获取两条记录。此外,我们在连接中设置了 buffered=True。 Cursor() 方法避免 MySQL Unread 结果错误。

About mysql-connector selections

它应该看起来像这样:cursor = db.cursor(buffered=True) 但是,在尝试使用准备好的 SQL 时,这可能会有点痛苦。

【讨论】:

  • 我收到一个错误 TypeError: cursor() got an unexpected keyword argument 'buffered'
  • 奇怪。也许您正在使用不同的库。平时选择有没有报错?
  • 不,它工作正常,但当我插入新数据时它不会改变值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多