string formatting using the % operator

W

William Gill

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
 
F

fargo

William said:
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
 
H

harold fellermann

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?
'%here you go%'

Cheers,

- harold -
 
D

Dan Sommers

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
 
W

William Gill

Dan said:
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!
 
P

Peter Hansen

Dan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,241
Messages
2,571,223
Members
47,858
Latest member
SangC9100

Latest Threads

Top