【问题标题】:How to avoid the below error in Python Flask如何避免 Python Flask 中的以下错误
【发布时间】:2019-01-02 10:46:31
【问题描述】:

我正在使用以下 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


【解决方案1】:

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

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

【讨论】:

  • 您好,谢谢您的回复。已如上更新结果声明。但结果还是一样。请求您的帮助以解决此问题。
  • 也许加逗号所以它是一个元组? (CR_ID,)(类似于question
猜你喜欢
  • 2019-12-18
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2010-10-26
相关资源
最近更新 更多