Python cx_Oracle中的Oracle准备语句的IN子句
问题内容:
我想在Python中使用cx_Oracle将IN子句与准备好的Oracle语句一起使用。
例如查询- select name from employee where id in ('101', '102', '103')
在python方面,我有一个列表[101, 102, 103]
,该列表已转换为这样的字符串,('101', '102', '103')
并在python中使用了以下代码-
import cx_Oracle
ids = [101, 102, 103]
ALL_IDS = "('{0}')".format("','".join(map(str, ids)))
conn = cx_Oracle.connect('username', 'pass', 'schema')
cursor = conn.cursor()
results = cursor.execute('select name from employee where id in :id_list', id_list=ALL_IDS)
names = [x[0] for x in cursor.description]
rows = results.fetchall()
这行不通。难道我做错了什么?
问题答案:
Oracle不支持该概念-而且您绝对不是尝试这种方法的第一人!您必须:
- 为每个值创建单独的绑定变量-在Python中做起来相当简单直接
-
如下所示,在Oracle类型上使用强制转换运算符创建子查询:https : //asktom.oracle.com/pls/asktom/f?p=100 :11:0 ::::: p11_question_id: 210612357425
-
使用存储过程来接受数组并直接在PL / SQL中执行多个查询
-
或完全做其他事情!