这是烧瓶应用程序的Python代码:
from flask import Flask, render_template, request
from sqlalchemy import create_engine
import pymysql
db = create_engine("mysql+pymysql://root:H@r$h123@localhost:3306/database")
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=["POST"])
def success():
name = request.form.get("name")
email = request.form.get("username")
password = request.form.get("password")
rpassword = request.form.get("rpassword")
db.execute("INSERT INTO information(name, email, password, rpassword)
VALUES(name, email, password, rpassword)",
{'name':name, 'email':email, 'password':password,
'rpassword':rpassword})
return render_template("success.html", name=name)
这是HTML代码
<form id="sign" action="{{url_for('success')}}" method="POST">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
Confirm-Password:<input type="password" name="rpassword"><br><br>
<input type="Submit" name="submit" value="Sign Up">
</form>
每当我尝试使用HTML表单添加数据时,我都可以成功输入数据,并且success.html也会加载,但数据不会在MySQL中显示
任何建议??
答案 0 :(得分:0)
请阅读文档。 https://www.python.org/dev/peps/pep-0249/#paramstyle
可能对于关键字参数应使用:
"INSERT INTO information(name, email, password, rpassword) VALUES(%(name)s, %(email)s, %(password)s, %(rpassword)s)"
答案 1 :(得分:0)
当它是DML命令时,您需要提交。
https://docs.sqlalchemy.org/en/latest/core/connections.html#using-the-threadlocal-execution-strategy
try:
db.execute(...)
db.commit()
except:
db.rollback()