I am using the % operator to create queries for a db app. It works fine
when exact strings, or numbers are used, but some queries need partial
matching that use the '%' as a wildcards. So for example the resultant
string should be 'WHERE name LIKE %smith%' (would match silversmith,
smithy, and smith). Is there any way to get something like
searchterm = 'smith'
sql += 'WHERE name LIKE %s' % searchterm
to return 'WHERE name LIKE %smith%' I have tried using escapes,
character codes for the % sign, and lots of other gyrations with no
success. The only thing that works is if I modify searchterm first:
searchterm = 'smith'
searchterm ='%'+'smith'+'%'
sql += 'WHERE name LIKE %s' % searchterm
Any Ideas?
Thanks,
Bill 5 1475
William Gill wrote: I am using the % operator to create queries for a db app. It works fine when exact strings, or numbers are used, but some queries need partial matching that use the '%' as a wildcards. So for example the resultant string should be 'WHERE name LIKE %smith%' (would match silversmith, smithy, and smith). Is there any way to get something like
searchterm = 'smith' sql += 'WHERE name LIKE %s' % searchterm
to return 'WHERE name LIKE %smith%' I have tried using escapes, character codes for the % sign, and lots of other gyrations with no success. The only thing that works is if I modify searchterm first:
searchterm = 'smith' searchterm ='%'+'smith'+'%' sql += 'WHERE name LIKE %s' % searchterm
Any Ideas?
try this :
sql += 'WHERE name LIKE %%%s%%' % searchterm
> to return 'WHERE name LIKE %smith%' I have tried using escapes, character codes for the % sign, and lots of other gyrations with no success. The only thing that works is if I modify searchterm first:
searchterm = 'smith' searchterm ='%'+'smith'+'%' sql += 'WHERE name LIKE %s' % searchterm
Any Ideas? "%%%s%%" % "here you go"
'%here you go%'
Cheers,
- harold -
--
If your only tool is a hammer, every problem looks like a nail.
--
On Mon, 13 Jun 2005 15:12:54 GMT,
William Gill <no*****@gcgroup.net> wrote: I am using the % operator to create queries for a db app. It works fine when exact strings, or numbers are used, but some queries need partial matching that use the '%' as a wildcards. So for example the resultant string should be 'WHERE name LIKE %smith%' (would match silversmith, smithy, and smith). Is there any way to get something like
searchterm = 'smith' sql += 'WHERE name LIKE %s' % searchterm
to return 'WHERE name LIKE %smith%' I have tried using escapes, character codes for the % sign, and lots of other gyrations with no success. The only thing that works is if I modify searchterm first:
searchterm = 'smith' searchterm ='%'+'smith'+'%' sql += 'WHERE name LIKE %s' % searchterm
Any Ideas?
Let the DB-API do more work for you:
cursor = connection.cursor( )
sql = """SELECT column2, columns3 FROM table WHERE name LIKE %s"""
values = ('%%%s%%' % searchterm,) # note that this is a tuple
cursor.execute( sql, values )
HTH,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Dan Sommers wrote: On Mon, 13 Jun 2005 15:12:54 GMT, William Gill <no*****@gcgroup.net> wrote:
I am using the % operator to create queries for a db app. It works fine when exact strings, or numbers are used, but some queries need partial matching that use the '%' as a wildcards. So for example the resultant string should be 'WHERE name LIKE %smith%' (would match silversmith, smithy, and smith). Is there any way to get something like
searchterm = 'smith' sql += 'WHERE name LIKE %s' % searchterm
to return 'WHERE name LIKE %smith%' I have tried using escapes, character codes for the % sign, and lots of other gyrations with no success. The only thing that works is if I modify searchterm first:
searchterm = 'smith' searchterm ='%'+'smith'+'%' sql += 'WHERE name LIKE %s' % searchterm
Any Ideas?
Let the DB-API do more work for you:
cursor = connection.cursor( ) sql = """SELECT column2, columns3 FROM table WHERE name LIKE %s""" values = ('%%%s%%' % searchterm,) # note that this is a tuple cursor.execute( sql, values )
HTH, Dan
I can't tell you how many times I looked at the table of format codes
and missed this.
Thanks everyone!
Dan Sommers wrote: Let the DB-API do more work for you:
cursor = connection.cursor( ) sql = """SELECT column2, columns3 FROM table WHERE name LIKE %s""" values = ('%%%s%%' % searchterm,) # note that this is a tuple
It looks like this might be a rare case where not using the % operator
might make it easier to see what's going on:
values = ('%' + searchterm + '%',)
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Philips |
last post by:
Consider the following simple dictionary
e={1:'one', 2: 'two'}
e
>>>'one'
However, If I attempt to print e using a formatted string
print " %(1)s" %e,
I get a KeyError: '1'
|
by: angellian |
last post by:
Sorry to raise a stupid question but I tried many methods which did
work.
how can I conserve the initial zero when I try to convert STR(06) into
string in SQL statment?
It always gives me 6...
|
by: James |
last post by:
Which is better, which is faster, which is easier etc... ?????
String.Format ( "yadda {0} yadda {1}", x, y )
"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of...
|
by: subramanian100in |
last post by:
Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
int main()
|
by: schoedl |
last post by:
Hello,
we often compose strings via a ostringstream and then create a string
from it. What is the rationale of not being able to use string in
place of a ostringstream, so I could write
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |