簡體   English   中英

如何使用 flask 應用程序中的 MySQL 將用戶輸入的數據存儲在我的數據庫中。 我收到一個錯誤

[英]How to store data in my database from a user Input using MySQL in flask app. I am getting an Error

源代碼如下:

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

嘗試:

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

更多詳情請參考這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM