query = """select * from mytable where mycol1=%s and mycol2 like '%file%'"""
as an attempt to use a float placeholder (and complains that a float arg is missing) when executed, as in:
cursor.execute( query, ( 'foo',) )
and '%%file%' doesn't work; mysqldb returns "unsupported format character"...
File "myapp.py", line 236, in handle_data
cursor.execute( query, param )
File "/usr/local/python/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
self.errorhandler(self, exc, value)
File "/usr/local/python/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
raise errorclass, errorvalue
ValueError: unsupported format character ''' (0x27) at index 585
I tried other obvious escape chars with no luck. Googling this problem, I saw various heroic efforts using string substitution (as in cursor.execute( query % some_perverse_string ), but it's too much of a hack for me and I'd rather not use that approach for the actual (rather complex) query in my app.
I ended up using regexp (...mycol2 regexp 'file'), which I guess is fine, but is there a way to use mysql wildcards and mysqldb placeholders ?
Thanks again for your help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Andy,
mysqldb interprets a mysql wildcard as in
query = """select * from mytable where mycol1=%s and mycol2 like '%file%'"""
as an attempt to use a float placeholder (and complains that a float arg is missing) when executed, as in:
cursor.execute( query, ( 'foo',) )
and '%%file%' doesn't work; mysqldb returns "unsupported format character"...
File "myapp.py", line 236, in handle_data
cursor.execute( query, param )
File "/usr/local/python/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
self.errorhandler(self, exc, value)
File "/usr/local/python/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
raise errorclass, errorvalue
ValueError: unsupported format character ''' (0x27) at index 585
I tried other obvious escape chars with no luck. Googling this problem, I saw various heroic efforts using string substitution (as in cursor.execute( query % some_perverse_string ), but it's too much of a hack for me and I'd rather not use that approach for the actual (rather complex) query in my app.
I ended up using regexp (...mycol2 regexp 'file'), which I guess is fine, but is there a way to use mysql wildcards and mysqldb placeholders ?
Thanks again for your help.
So close.
%%file%%
Or the more sensible approach:
query = """select * from mytable where mycol1=%s and mycol2 like %s"""
matchfile = "%file%"
cursor.execute( query, ( 'foo', matchfile) )