cx_Oracle clause IN using a variable

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 16 13:04:21 EDT 2012


On Tue, Oct 16, 2012 at 7:41 AM, Beppe <giuseppecostanzi at gmail.com> wrote:
> Hi all,
> I don't know if it is the correct place to set this question, however,

The best place to ask questions about cx_Oracle would be the
cx-oracle-users mailing list.

> what is wrong?
> suggestions?

With the bind parameter you're only passing in a single string, so
your query is effectively equivalent to:

SELECT field1,field2,field3
FROM my_table
WHERE field_3 IN ('CNI,CNP')

You can't pass an actual list into a bind parameter the way that you
would like.  You need to use a separate parameter for each item in the
list.  This may mean constructing the query dynamically:

in_vars = ','.join(':%d' % i for i in xrange(len(sequence_of_args)))

sql = """
SELECT field1,field2,field3
FROM my_table
WHERE field_3 IN (%s)
""" % in_vars

cursor.execute(sql, sequence_of_args)



More information about the Python-list mailing list