python - Escape string within % -


this question has answer here:

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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -