Python 如何在mysql中使用hashtag查询字符串
我想将书名作为参数传递,并获取对该书的推荐Python 如何在mysql中使用hashtag查询字符串,python,mysql,sql,flask,Python,Mysql,Sql,Flask,我想将书名作为参数传递,并获取对该书的推荐 @app.route('/book_details/<book_title>') def book_details(book_title): cur = mysql.connection.cursor() title = book_title.replace("'", "\\'") title = title.replace("&", "\
@app.route('/book_details/<book_title>')
def book_details(book_title):
cur = mysql.connection.cursor()
title = book_title.replace("'", "\\'")
title = title.replace("&", "\\&")
title = title.replace("#", "\\#")
resultValue = cur.execute(f"SELECT * FROM `books` WHERE title = '{title}' LIMIT 1")
if resultValue > 0:
rec_book_list = output_recommendations(book_title)
book = cur.fetchall()
return render_template('testDetails.html', book = book, book_list = rec_book_list)
return 'No book'
这适用于带有单引号和符号的书名,但如果书名包含hashtag,则不起作用。如果标题中有错误,函数将返回“No book”
如何查询其中包含的字符串?我将删除所有转义代码,并让连接器执行:cur.executeSELECT*从“其中title=%s LIMIT 1,title,`”一书中。尝试了此操作。适用于除hashtag之外的所有特殊字符我找到了解决方案。和传递hashtag和其他特殊字符不同,传递字符的URL编码是有效的