将cx_Oracle与IN语句一起使用(Python 3)

问题描述 投票:0回答:1

我试图使用cx_Oracle将参数传递到SQL“IN”语句。这给出了正确的结果:

sql = """select * from
           (select level numb from dual connect by level <= 4)
         where numb = :var"""

print([row[0] for row in cur.execute(sql, (1,))])
Output: [1]

但是我无法弄清楚如何使用“IN”语句。

sql = """select * from
           (select level numb from dual connect by level <= 4)
         where numb in :var"""

print([row[0] for row in cur.execute(sql, (1, 2))])
Output: cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

我尝试过IN语句的变体,还使用字典传递参数。

python python-3.x oracle cx-oracle bind-variables
1个回答
0
投票

使用单个值或文字时,SQL中的IN子句需要括在括号中的值。由于您传递了两个参数,因此在paranetheses中包含两个占位符。

sql = """select * from
           (select level numb from dual connect by level <= 4)
         where numb in (:1, :2)"""

print([row[0] for row in cur.execute(sql, (1, 2))])
© www.soinside.com 2019 - 2024. All rights reserved.