如何在flask应用程序中使用MySQL将用户输入的数据存储在我的数据库中。

源代码如下:

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)")

猜你喜欢

转载自blog.csdn.net/babyai996/article/details/131986911