【问题标题】:How to store data in my database from a user Input using MySQL in flask app. I am getting an Error如何在烧瓶应用程序中使用 MySQL 将用户输入的数据存储在我的数据库中。我收到一个错误
【发布时间】:2020-08-04 05:28:53
【问题描述】:

源码如下:

from flask import Flask, render_template, request
from flask_mysqldb import MySQL
from MySQLdb import cursors

app = Flask(__name__)

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = Sorry, can't show it.
app.config['MYSQL_DB'] = Sorry, can't show it.

mysql = MySQL(app)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        userdetail = request.form
        username = userdetail['username']
        password = userdetail['user_password']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO userinfo(username, user_password) VALUES(%s, %s),(username, user_password)")
        mysql.connection.commit()
        cur.close()
        return 'SUCCESS'
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)

这是我的代码,我在这里尝试从用户那里获取数据并将其存储在我的数据库中,但我收到了一个错误:

MySQLdb._exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s),(username, user_password)' at line 1")

它实际上是在这一行显示错误:

cur.execute("INSERT INTO userinfo(username, user_password) VALUES(%s, %s),(username, user_password)")

【问题讨论】:

    标签: python mysql flask


    【解决方案1】:

    尝试:

    cur.execute("INSERT INTO userinfo(username, user_password) VALUES(%s, %s)",(username, user_password))
    

    更多详情请参考here

    【讨论】:

    • 完成。其实,问题很简单。而不是'user_password',我应该输入'password',因为这是我获取密码的变量。无论如何,感谢您的帮助,MYSQL 文档可以挽救生命。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多