python - Escape string within % -
this question has answer here:
- how escape % python mysql query 1 answer
i use pymysql query mysql database in python:
filter = "pe" connection = pymysql.connect(host="x", user="x", password="x", db="x", port=3306, cursorclass=pymysql.cursors.sscursor) cursor = connection.cursor() sqlquery = "select * usertable name '%%s%'" cursor.execute(sql, (filter)) response = cursor.fetchall() connection.close()
this returns nothing. write:
sqlquery = "select * usertable name '%" + filter +"%'"
and execute: cursor.execute(sql)
, lose escaping, makes program vulnerable injection attacks, right?
is there way insert value without losing escape?
...where name '%%%s%%'"
not work. think %s adds ' on both sides of replaced escaped string part of function within pymysql.
you need pass whole pattern query parameter, , use tuple:
filter = "%pe%" sql = "select * usertable name %s" cursor.execute(sql, (filter,))
Comments
Post a Comment