首页 > 解决方案 > 如何避免 Python Flask 中的以下错误

问题描述

我正在使用下面的 3 个 html 即 Index.html , Results.html 和 Edit.html 和 python 代码文件 app.py 代码也粘贴在下面。保存到数据库工作正常,当我检索数据进行编辑并单击提交时,我遇到以下错误“_mysql_exceptions.ProgrammingError: (1064,“您的 SQL 语法有错误;请查看对应的手册您的 MySQL 服务器版本,以便在第 1 行的 '%s' 附近使用正确的语法")"

Index.html

<h1>Welcome</h1>
<br>

<br>

<form method="POST" action="{{ url_for('index') }}">

Name <input type="text" name="name" />
  <br>

Email <input type="email" name="email" />
  <br> 
CRType <select name = "CRType">
        <option value = "New">New</option>
         <option value = "Old">Old</option>
  </select><br>  
<input type="submit" value="Submit">
</form>

Results.html

<a href = "/">Add CR</a>
<table border = 1>
  {% for user in userDetails %}
  <tr>
  <td> {{user[3]}} </td>
   <td> {{user[0]}} </td>
    <td> {{user[1]}} </td>
  <td> {{user[2]}} </td>
  <td>  <a href="/Edit?CR_ID={{user[3]}}"> Edit Profile</a> </td>
 </tr>
  {% endfor %}
</table>

Edit.html

h1>Welcome to Update Zone</h1>
<br>

<br>
<body>
<form method="POST" action="{{ url_for('Edit') }}">

CR_ID <input type="text" name="CR_ID" value = "{{user[0]}}"/>
  <br>
Name <input type="text" name="name" value = "{{user[1]}}"/>
  <br>

Email <input type="email" name="email" value = "{{user[2]}}"/>
  <br>
  <br>
      <input type="submit" value="Submit">
</body>
</form>

App.PY

from flask import Flask, render_template, request, redirect
from flask_mysqldb import MySQL
# from flask_table import Table, Col, LinkCol



app = Flask(__name__)

# Configure db
#db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
#app.config['MYSQL_PASSWORD'] = 'P@$$w0rd'
app.config['MYSQL_DB'] = 'flaskapp'

mysql = MySQL(app)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # Fetch form data
        userDetails = request.form
        name = userDetails['name']
        email = userDetails['email']
        CRType = userDetails['CRType']
     #   CR_ID = userDetails['CR_ID']
        cur = mysql.connection.cursor()
      #  cur.execute("""INSERT INTO users(name, email, CRType) VALUES(%s, %s, % )""",(name, email, CRType)
        cur.execute("""INSERT INTO users (name, email, CRType) VALUES (%s, %s, %s)""", (name, email, CRType))
        mysql.connection.commit()
        cur.close()
        return redirect ('/results')
      #  return redirect('/results')
    return render_template('index.html')

@app.route('/results')
def results():
    cur = mysql.connection.cursor()
    resultValue = cur.execute("SELECT * from users")
    if resultValue > 0:
        userDetails = cur.fetchall()
    #    edit = LinkCol('Edit', 'edit', url_kwargs=dict(id='CR_ID'))
        return render_template('results.html',userDetails=userDetails)


@app.route('/Edit', methods=['GET', 'POST'])
def Edit():
       # request.method == 'GET':
        # Fetch form data
      #  user = request.form
      #  CR_ID = user['CR_ID']
        CR_ID = request.args.get('CR_ID')
        name = request.args.get('name')
        email = request.args.get('email')
        CRType = request.args.get('CRType')
        cur = mysql.connection.cursor()
      #  result= cur.execute("SELECT CR_ID, name, email  from users where CR_ID = 1")
        result = cur.execute("""SELECT CR_ID, name, email from users where CR_ID = %s""",CR_ID)
     #   result = cur.execute("Update table users set name=?, email=?, CRType=? where CR_ID = %s", CR_ID)
        RV = cur.fetchall()
        user = RV[0]
        cur.close()
        return render_template('Edit.html',user=user)

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

标签: python

解决方案


您的 SQL 语句格式不正确。它应该是

result = cur.execute("SELECT CR_ID, name, email from users where CR_ID = %s", (CR_ID))

推荐阅读