[PIPython] problema tuple

Mariano Di Felice mardif
Mar 12 Apr 2005 15:43:18 CEST


Matteo Giacomazzi ha scritto:

> Ciao Mariano,
>
>> avendo la seguente tupla:
>>
>> t = [1000,1001]
>>
>> e la seguente query:
>>
>> "select * from <table> where <column> in %s" % tuple([x for x in t])
>>
>> il risultato sarà:
>>
>> select * from <table> where <column> in (1000,1001)
>
>
> tuple([x for x in t])
> è equivalente a
> tuple(t)
> Effettivamente t è una lista e non una tupla.
>
>> Il problema sorge quando nella tupla c'è un solo elemento, il che 
>> genererà nella query:
>>
>> select * from <table> where <column> in (1000, )
>>
>> dando naturalmente errore di SQL.
>>
>> Soluzioni?
>
>
> Puoi fare così:
>
> "select * from <table> where <column> in (%s)" % ','.join(t)
>
> nel caso di un solo elemento in t risulterà comunque una query 
> sintatticamente valida.
> In verità il modo migliore sarebbe:
>
> "select * from <table> where <column> in (%s)" % ','.join(['%s']*len(t))
>
> e poi passare tuple(t) come parametro al metodo execute del cursore 
> (così eventuali escape vengono gestiti dal modulo del DB).
>
> Ciao,
>   Matteo
>
>------------------------------------------------------------------------
>
>_______________________________________________
>La mailing list di python.it
>python a lists.python.it
>http://lists.python.it/cgi-bin/mailman/listinfo/python
>  
>
Grazie per la reply

Ho trovato anche un altro modo:

"select * from <table> where <column> in %s" % tuple([x for x in t])[1:-1]

che ne dite???

-- 
Se hai urgenza, puoi rispondere alla mia e-mail con un SMS gratuito!
http://www.areaprivati.190.it/190/trilogy/jsp/dispatcher.do?ty_key=rsms_invio_sms_pri&klink=6E2DD8940A016EF3006811DF52128EF2

/\/\ariano Di Felice
Java PHP Python programmer
with MySQL, PostgreSql, SQLite and Oracle support
Linux Platform Developer
mariano.difelice a eprometeus.com
www.eprometeus.com
Tel. 0735 656881
Fax. 0735 656307
---------------------------------------------------------
NO ai brevetti software! http://www.nosoftwarepatents.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: mardif.vcf
Type: text/x-vcard
Size: 274 bytes
Desc: not available
Url : http://list.zope.it/pipermail/python/attachments/20050412/c46bb081/mardif.vcf


More information about the Python mailing list