473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 18 '05 #1
5 19741
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


Jul 18 '05 #2
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

Jul 18 '05 #3
> >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


Jul 18 '05 #4
Dave Harrison <da**@nullcube.com> wrote in message news:<ma**********************************@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?
Jul 18 '05 #5
Dave Harrison <da**@nullcube.com> wrote in message news:<ma**********************************@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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: Derek Fountain | last post by:
I was trying to use MySQLdb to connect to a database. All is OK, except I can't figure out how to get the details of an error. Suppose I try to connect to a non existant server, or with the wrong...
0
by: Stephen Roderick | last post by:
Getting weird error, and I was wondering if anyone had any suggestions. My system appears to satisfy all MySQLdb prerequisities in terms of python, mysql, etc. Also, phpMyAdmin works fine with...
21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
2
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py...
4
by: fedor | last post by:
Hi all, I have a problem with mysql connections. After about 28000-29000 connections, I get a "Can't connect to MySQL server on '127.0.0.1'" error. I have made a small program which generates...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
0
by: jgarber | last post by:
Hello, I just upgraded MySQLdb to the 1.2.0 version provided by Redhat Enterprise Linux ES4. At that point I began to get segfaults when importing twisted after MySQLdb, but not before. --...
0
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to...
2
by: Nikhil | last post by:
I am using the MySQLdb python module. I have a table named 'testing' with few columns, under the 'test' database, what is hosted on a remote mysql server. I want to run the following query to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.