2

我正在尝试使用 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 语句的变体,也尝试过使用字典来传递参数。

4

1 回答 1

1

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

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))])
于 2018-09-06T18:35:38.457 回答