我有一個Flask
網站,其後端爲MySQL
。我有一個叫做用戶的表格。它有兩列:用戶名和名稱,一個記錄:MySQLdb在與Flask Post一起使用時返回舊數據
name username
Jim testuser123
當用戶點擊網站上的按鈕,它更新記錄的名稱設置爲Bob
然後打印所有記錄name = 'Bob'
。但是,它不會返回任何結果。如果我在重新查詢之前刷新連接,那麼它確實會返回一個結果。 mdb.connect
對象緩存數據嗎?它怎麼會不能返回正確的結果?
初始化的.py:
import pandas as pd
import MySQLdb as mdb
from flask import Flask, render_template, request
def sql_con():
return mdb.connect(host='myhost', port=3306, user='root', passwd='root', db='db', use_unicode=True, charset="utf8")
app = Flask(__name__)
def update_record():
con = sql_con()
cur = con.cursor()
sql_string= "Update users set name = 'Bob' where username = 'testuser123'"
cur.execute(sql_string)
con.commit()
@app.route('/', methods=['GET', 'POST'])
def myroute():
con = sql_con()
if request.method == 'POST':
update_record()
print pd.read_sql("select * from users where name = 'Bob'", con=con)
return render_template('1.html')
app.run(debug=True, port=5050)
1.HTML
<html>
<body>
<form method="POST">
<button id="mybutton" name='btn' value="mybutton">Submit Data</button>
</form>
</body>
對於這個代碼打印一個結果,我必須補充con=sql_con()
我叫update()
之後,但在print
聲明之前。這是爲什麼?
可能與https://stackoverflow.com/questions/29438471/how-to-prevent-pandas-psql-read-sql-query-from-fetching-cache相關 –
可能重複[爲什麼選擇一些mysql連接舊數據mysql數據庫後刪除+插入?](https://stackoverflow.com/questions/9318347/why-are-some-mysql-connections-selecting-old-data-the-mysql-database-after-a -del) –