Menu

mysql wildcard v mysqldb placeholder

Help
sevzn
2005-11-20
2012-09-19
  • sevzn

    sevzn - 2005-11-20

    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.

     
    • Andy Dustman

      Andy Dustman - 2005-11-20

      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) )

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.