我试图在现有数据库中添加数据但得到错误输出,有一个小错误请帮助我。谢谢
代码 ::
@app.route('/')
def new_student():
return render_template('student.html')
@app.route('/addrec', methods=['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
with sql.connect("database.db") as con:
cur = con.cursor()
cur.execute('''INSERT INTO students (name,addr,city,pin)
VALUES(?, ?, ?, ?)''',(nm,addr,city,pin))
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("result.html", msg=msg)
con.close()
if __name__ == '__main__':
app.run(debug=True)
我的 result.html 文件是:
<!doctype html>
<html>
<body>
result of addition : {{ msg }}
<h2><a href = "\">go back to home page</a></h2>
</body>
</html>
输出应该是::“记录已成功添加”,但我收到“插入操作中的错误”。
qq_花开花谢_0
梵蒂冈之花
慕斯王
相关分类