使用MySQL数据刷新烧瓶更新

时间:2019-03-18 19:21:35

标签: python mysql python-3.x flask

我正在尝试使烧瓶的网页显示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)

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

1 个答案:

答案 0 :(得分: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)