[mod_python] String format in "LIKE" clause

Luis M. Gonzalez luismg at gmx.net
Thu Jan 26 18:33:58 EST 2006


Hi folks,

I know this is not specific for mod_python, but perhaps someone could give me a hint with this problem:

I have a method that accepts a parameter, and then performs an SQL query withe a LIKE clause as follows:

def query(req, company):        
    import MySQLdb
    db = MySQLdb.connect(host="localhost", user="root", passwd="", db="vic")
    cursor = db.cursor()
    cursor.execute(" select * from customers where company like '%s' "  %company )
    ...

The problem is that I want the query to accept partial company names, as well as the full ones.
For example, lets say that there's a company called "ACME INDUSTRIES LTDA" and I just input "INDUSTRIES".

In SQL, using something like "%industries%" in the like clause would be ok.
The problem is that Python also uses the percent sign (%) for string formatting, and I don't know how to write a LIKE clause that accepts a string parameter.

I tried this:
("select * from customers where company like %s%"  %company ) # doesn't work
("select * from customers where company like '%(%s)%"  %company  # doesn't work either

The question is: 
How should a include the parameter into de LIKE clause in order to accept only a part of the full company name and return the matching results?

I hope I made myself clear...(sorry for my english)

Thanks in advance!
Luis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060126/cd4caa45/attachment-0001.html


More information about the Mod_python mailing list