Escape SQL "LIKE" value for Postgres with psycopg2

Posted by Evgeny on Stack Overflow See other posts from Stack Overflow or by Evgeny
Published on 2010-01-21T00:58:54Z Indexed on 2010/06/02 6:03 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

Does psycopg2 have a function for escaping the value of a LIKE operand for Postgres?

For example I may want to match strings that start with the string "20% of all", so I want to write something like this:

sql = '... WHERE ... LIKE %(myvalue)s'
cursor.fetchall(sql, { 'myvalue': escape_sql_like('20% of all') + '%' }

Is there an existing escape_sql_like function that I could plug in here?

(Similar question to How to quote a string value explicitly (Python DB API/Psycopg2), but I couldn't find an answer there.)

© Stack Overflow or respective owner

Related posts about python

Related posts about postgresql