我使用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)
,但是我失去了转义,这使得程序容易受到注入攻击,对吗?
有什么方法可以在不失去逃逸的情况下将值插入到类似的值中吗?
...WHERE name LIKE '%%%s%%'"
不起作用。我认为%s在被替换的转义字符串的两边添加‘,作为它在PyMySQL中函数的一部分。
发布于 2016-07-28 12:00:49
您需要将整个模式作为查询参数传递,并使用tuple。
filter = "%Pe%"
sql = "SELECT * FROM usertable WHERE name LIKE %s"
cursor.execute(sql, (filter,))
发布于 2016-07-28 11:56:27
是你想要的%
的两倍。
sqlquery = "SELECT * FROM usertable WHERE name LIKE '%%%s%%'"
https://stackoverflow.com/questions/38635578
复制相似问题