[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: parametrizing a sqlite query

Dennis Lee Bieber

2/24/2010 6:41:00 PM

On Wed, 24 Feb 2010 14:07:07 -0300, Sebastian Bassi
<sbassi@clubdelarazon.org> declaimed the following in
gmane.comp.python.general:

> c.execute("SELECT bin FROM bins WHERE qtl LIKE '%:keys%'",{'keys':keywords})
>
> This query returns empty. When it is executed, keywords = 'harvest'.
> To check it, I do it on the command line and it works as expected:
>
> sqlite> SELECT bin FROM bins WHERE qtl LIKE '%harvest%';
> 11C
> 11D
> 12F
>
> I guess there is a problem with the "%".

No there isn't... The problem is that you need to put the wildcards
into the parameter instead of the placeholder.

c.execute("select bin from bins where qtl like :keys",
{"keys" : "%" + keywords + "%"} )

Note -- no quotes IN the SQL... any quotes and escaping of data is
done before the data value is put into the query. Your original
formulation will result in:

select bin from bins where qtl like '%'harvest'%'

See the extra quotes?
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/