Like this question passing-param-to-db-execute-for-where-in-int-list
But more where condition, how to deal with it?
select * from tbl where params1='p1' and params2='p2' and params3 in ('v1', 'v2', v3')
If I want pass params1, params2, params3 to SQL statement using Python DB API, any suggestion?
BTW: Database is Oracle
You need to use SQL parameters for each value.
For the
in
statement, that means you need to generate the parameters:where I assume that
params3_value
is a list of values to testparams3
against. Ifparams3_value
is 3 elements (like['v1', 'v2', 'v3']
) then the generated SQL will look like:Then pass those paramaters to the
cursor.execute()
call:I used the
:name
named SQL parameter style here as that is what thecx_Oracle
uses. Consult your database connector documentation for the exact supported parameter styles.The
:named
named SQL parameter style requires that you pass in parameters as a dictionary, so the above code generates the right keys for theparams3_value
items.