SQLAlchemy使用字典插入值

时间:2018-11-24 08:36:49

标签: python dictionary flask sqlalchemy insert

用flask执行以下代码时,出现此内部服务器错误。有人有解决这个问题的想法吗?提前谢谢!

错误消息:

  

sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)   “用户”またはその近辺で构文エラー第1行:INSERT INTO用户(名称,密码)VALUES   (“ nishino”,“ nishin ...                       ^ [SQL:'INSERT INTO user(name,password)VALUES(%(name)s,%(password)s)'] [参数:{'name':'nishino',   'password':'nishino'}](此错误的背景位于:   http://sqlalche.me/e/f405

代码:

@app.route("/success", methods=["POST"])
def success():
    if not request.form.get("username") or not request.form.get("password"):
        return render_template("failure.html", errormsg="sorry something wrong. Please try again.")
    name = request.form.get("username")
    password = request.form.get("password")
    db.execute("INSERT INTO user (name, password) VALUES (:name, :password)",
                {"name":name, "password":password})
    sucesscoment = f"{name} is successfully registered!!"
    db.commit()
    return render_template("success.html", successcoment = successcoment)

0 个答案:

没有答案