using like and % in MySQLdb

Gerhard Häring gh at
Thu Aug 7 11:21:01 CEST 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. 

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

-- Gerhard

More information about the Python-list mailing list