我正在 Flask 中做一条基于 url 中的搜索参数的路由,该路由将过滤数据库返回所有类似的值,但我收到以下错误:near“%”:语法错误。有人可以帮忙吗?@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
1 回答
MM们
TA贡献1886条经验 获得超2个赞
您需要将字符串值用单引号引起来。使用参数更简单(也更高效):
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()
添加回答
举报
0/150
提交
取消