如何避免Python Flask中的以下错误

时间:2018-07-25 14:50:31

标签: python

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

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

    $(function () {
   "use strict";

$(document).ready(function(){
  $(".client").owlCarousel();
    loop:true;
    items:1;
    nav:true;
});
});

1 个答案:

答案 0 :(得分:0)

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

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