Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   using like and % in MySQLdb (http://www.velocityreviews.com/forums/t320686-using-like-and-in-mysqldb.html)

Dave Harrison 08-07-2003 04:38 AM

using like and % in MySQLdb
 
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


Torsten Marek 08-07-2003 08:15 AM

Re: using like and % in MySQLdb
 
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
>



=?ISO-8859-1?Q?Gerhard_H=E4ring?= 08-07-2003 10:09 AM

Re: using like and % in MySQLdb
 
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


Dave Harrison 08-07-2003 10:39 AM

Re: using like and % in MySQLdb
 
> >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.


hehe yeah Id been testing what I was doing against my mysql client and couldnt work out why the module kept adding the '', you're solution works a treat, thanks torsten

> >however this :
> >
> >self.curs.execute(
> > """SELECT * FROM user WHERE login LIKE '%dave%'"""
> > )
> >
> >does work
> >
> >so what's the go ?
> >cheers
> >Dave
> >

>
> --
> http://mail.python.org/mailman/listinfo/python-list



Chris 08-07-2003 03:07 PM

Re: using like and % in MySQLdb
 
Dave Harrison <dave@nullcube.com> wrote in message news:<mailman.1060232532.23485.python-list@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 ?



Try

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

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

I think you want the % stuff for LIKE to be part of the substituted in
string, not part of the query string. Make sense?

Chris 08-07-2003 03:14 PM

Re: using like and % in MySQLdb
 
Dave Harrison <dave@nullcube.com> wrote in message news:<mailman.1060232532.23485.python-list@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


All times are GMT. The time now is 12:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57