Variable argument list for IN clause
Oswaldo Hernández <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I have an SQL with an IN clause.
If the number of params are fixed there no problem,
>>> c = con.cursor()
>>> c.execute("select 1 in (%s, %s, %s) ", (1,2,3))
>>> c.fetchall()
[(True,)]
>>>
The problem is if i don't know the number of params.
I try to calculate it at run time an replace the marker:
>>> params = (1,2,3)
>>> SQL = "select 1 in (%s)"
>>> # count params and replace
>>> SQL = SQL % ", ".join(["%s" for p in params])
>>>
>>> c.execute(SQL, params)
>>> c.fetchall()
[(True,)]
But no work on more complex sql with another params out the IN clause.
How can avoid it? Is there any simple way?
Thanks,
--
*****************************************
Oswaldo Hernández
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.