我有一个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) –