using like and % in MySQLdb

Gerhard Häring gh at ghaering.de
Thu Aug 7 05:21:01 EDT 2003


Dave Harrison wrote:
> 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. [...]

It _will_ work. You'll have to escape the first and the last percent 
sign with another percent sign and leave the %s in the middle untouched. 
Thus:

"SELECT * FROM user WHERE login LIKE '%%%s%%'"

-- Gerhard





More information about the Python-list mailing list