You need to READ your SQL arguments correctly.
And by correctly quoting, I mean using the quote feature provided by DBAPI, rather than adding "around your line", which is useless.
The correct code is:
q = "%"+q+"%" cursor.execute( 'SELECT * FROM table WHERE field LIKE %s', (q,) )
Really correct code:
q = "%"+q.replace("%","%%")+"%" cursor.execute( 'SELECT * FROM table WHERE field LIKE %s', (q,) )
Suppose q = "a'bc" First rewrite this as "% a'bc%", then use it as a regular string argument. psycopg will rewrite it as "% a \ bc%", as it should be.
If q can contain "%" and you want to find it, use the second.
source share