I am trying to format a SQL query in python by supplying arguments that are taken from a method like this
query = "select * from employees where employee_name like '%s%'" % (name)
When I run this code, I get the following error (python 2.6)
ValueError: unsupported format character ''' (0x27) at index 886
I tried this as well in command line to figure out the problem
>>> print "hello '%s'" % ('world')
hello 'world'
>>> print "hello '%s\%'" % ('world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> print "hello '%s%'" % ('world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>>
The %s works when I don't add a % within the single quotes immediately, but breaks when I add a % after that even with escaping. Is there a remedy for this, I can't subsitute variables within an SQL query without this.