using like and % in MySQLdb

Chris bit_bucket5 at hotmail.com
Thu Aug 7 17:14:45 CEST 2003


Dave Harrison <dave at nullcube.com> wrote in message news:<mailman.1060232532.23485.python-list at python.org>...
> Im sure this is a really obvious problem but :
> 
> self.curs.execute(
>         """SELECT * FROM user WHERE login LIKE '%%s%'""", [login]
>         )
> 
> will not work ... gives me an "unsupported format character ''' (0x27)"
> 
> escaping the %'s with % as the doco recommends wont work either.
> 
> however this :
> 
> self.curs.execute(
>         """SELECT * FROM user WHERE login LIKE '%dave%'"""
>         )
> 
> does work
> 
> so what's the go ?
> cheers
> Dave


I just posted a reply and realized I made one mistake.  I said to use

"select * from user where login like %s" % ('%%%s%%' % login)

But it should be like

"select * from user where login like %s", ('%%%s%%' % login)

as in 

self.curs.execute("select * from user where login like %s", ('%%%s%%' % login))

That should work.

-Chris




More information about the Python-list mailing list