【问题标题】:Flask update on refresh with data from MySQL使用来自 MySQL 的数据刷新烧瓶更新
【发布时间】:2023-03-13 16:31:01
【问题描述】:

我正在尝试使用烧瓶制作网页,该网页显示来自 mysql 表的数据。我的代码是:

@app.route("/")
def index():
    cursor = db.cursor()
    cursor.execute('SELECT * from private')
    privateDB = cursor.fetchall()
    cursor.close()

    return render_template('index.html', t=privateDB)

但是,每当我刷新页面时,我都会获取旧数据,它不会获取新的更新数据。怎么修?谢谢。

【问题讨论】:

    标签: python mysql python-3.x flask


    【解决方案1】:

    用这段代码试试

    @app.route("/")
    def index():
        connection = mysql.connect()
        cursor = connection.cursor()
        cursor.execute("SELECT * from private")
        data = cursor.fetchall()
    
        return render_template('index.html', data=data)
    

    【讨论】:

    • 是的。将 .connect() 移动到该函数有帮助。谢谢
    • 这不是一个坏习惯吗?必须为每个 app.route 打开一个新连接?
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2018-12-25
    • 2013-05-26
    相关资源
    最近更新 更多