using like and % in MySQLdb

Torsten Marek shlomme at gmx.net
Thu Aug 7 04:15:55 EDT 2003


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+"%", ))

The problem with escaping the % characters is, that MySQLdb converts
self.curs.execute("""SELECT * FROM user WHERE login LIKE '%%%s%%'""" , 
(login,))
to
"SELECT * FROM user WHERE login LIKE '%'dave'%'"
and I don't know how to prevent this.
> however this :
> 
> self.curs.execute(
>         """SELECT * FROM user WHERE login LIKE '%dave%'"""
>         )
> 
> does work
> 
> so what's the go ?
> cheers
> Dave
> 





More information about the Python-list mailing list