ltpitt
asked on
Python MySQLdb parametrized query
Hi everybody!
I need to write a parametrized mysql query with python using MySQLdb
I did this successfully:
cursor.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))
The problem comes when I want to do a LIKE with two parameters:
cursor.execute("SELECT * FROM foo WHERE bar like '%%s%' AND baz like '%%s%'", (param1, param2))
I get an error:
query = query % db.literal(args)
ValueError: unsupported format character ''' (0x27) at index 47
How can I write this correctly?
I need to write a parametrized mysql query with python using MySQLdb
I did this successfully:
cursor.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))
The problem comes when I want to do a LIKE with two parameters:
cursor.execute("SELECT * FROM foo WHERE bar like '%%s%' AND baz like '%%s%'", (param1, param2))
I get an error:
query = query % db.literal(args)
ValueError: unsupported format character ''' (0x27) at index 47
How can I write this correctly?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.