我正在flask中执行一个路由,该路由基于url中的搜索参数来过滤数据库,返回所有类似的值,但我得到了以下错误:接近"%":语法错误。有人能帮忙吗?
@app.route('/courses/<search>', methods=['GET'])
def searchg(search):
con = sqlite3.connect('data.db')
cur = con.cursor()
courses = f'select id,name,description,tags,adminId,content,image from courses where name LIKE %{search}%'
cur.execute(courses)
allcourses=cur.fetchall()
con.close()
courseList = []
for c in allcourses:
formattedcourses = {"id":c[0],"name":c[1],"description":c[2],"tags":eval(c[3]),"adminId":c[4],"content":eval(c[5].replace("#",'"')),"image":c[6]}
courseList.append(formattedcourses)
return jsonify(courseList),200
字符串值周围需要单引号。它更简单(更有效(的参数:
cur = con.cursor()
courses = f'select id,name,description,tags,adminId,content,image from courses where name LIKE ?'
cur.execute(courses, ('%' + search + '%',))
allcourses=cur.fetchall()