<分区>
我使用 PyMySQL 在 python 中从 MySQL 数据库查询:
filter = "Pe"
connection = pymysql.connect(host="X", user="X", password="X", db="X", port=3306, cursorclass=pymysql.cursors.SSCursor)
cursor = connection.cursor()
sqlquery = "SELECT * FROM usertable WHERE name LIKE '%%s%'"
cursor.execute(sql, (filter))
response = cursor.fetchall()
connection.close()
这不会返回任何内容。 我可以写:
sqlquery = "SELECT * FROM usertable WHERE name LIKE '%" + filter +"%'"
并执行:cursor.execute(sql)
,但随后我失去了转义,这使得程序容易受到注入(inject)攻击,对吧?
有没有办法可以在不丢失转义符的情况下将值插入 LIKE?
...WHERE name LIKE '%%%s%%'"
不起作用。我认为 %s 在替换的转义字符串的两侧添加 ' 作为其函数的一部分PyMySQL。