Flask使用MySQL将数据插入数据库

时间:2019-02-22 12:55:08

标签: mysql flask flask-mysql

我正在尝试将数据插入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)

2 个答案:

答案 0 :(得分:0)

尝试一下

mycursor.execute(`INSERT INTO railway (name) VALUES (?)`,(name));

答案 1 :(得分:0)

mycursor.execute(“”“”插入铁路(名称)值(%s)“”“,%(名称,)) 您忘记了%