0

当我尝试我的程序时,我试图将数据插入 MYSQL 数据库,但在下一行出现错误“没有足够的参数来格式化字符串”。

ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB se
rver version for the right syntax to use near '%s)' at line 1

我的代码

from flask import Flask, render_template, request
import mysql.connector

app = Flask(__name__, static_url_path="", static_folder="static")

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="railway"
)
mycursor = mydb.cursor()

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/store',methods=['POST', 'GET'])
def store():
   if request.method=='POST':
      name=request.form['name']
      mycursor.execute("""INSERT INTO railway (name) VALUES (%s)""",(name))
      mydb.commit()
      return "record inserted."
   else:
      return "s"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)
4

3 回答 3

0

mycursor.execute("""INSERT INTO rail (name) VALUES (%s)""", %(name,)) 你忘记了 %

于 2019-02-22T13:03:52.757 回答
0

尝试这个

mycursor.execute(`INSERT INTO railway (name) VALUES (?)`,(name));
于 2019-02-22T12:59:49.667 回答
0

在使用 C 样式格式说明符语法时,不要在 Values (,) 之后使用逗号。这是正确的语法。

mycursor.execute("INSERT INTO railway (name) VALUES (%s)"%(name,))
于 2021-10-06T10:32:26.023 回答