using like and % in MySQLdb

Gerhard Häring gh at ghaering.de
Thu Aug 7 12:09:38 CEST 2003


Torsten Marek wrote:
> Dave Harrison schrieb:
> 
>> 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.
>>
> Why don't you do
> self.curs.execute(
>     """SELECT * FROM user WHERE login LIKE %s""", ("%"+login+"%", ))
> [...]

You're right. Please ignore my previous post in this thread. It 
correctly shows how to escape percent signs, but won't work in the SQL 
context.

-- Gerhard





More information about the Python-list mailing list