Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cx_Oracle中Oracle准备语句的IN子句_Python_Oracle_Prepared Statement_Cx Oracle - Fatal编程技术网

Python cx_Oracle中Oracle准备语句的IN子句

Python cx_Oracle中Oracle准备语句的IN子句,python,oracle,prepared-statement,cx-oracle,Python,Oracle,Prepared Statement,Cx Oracle,我想在准备好的Oracle语句中使用IN子句,该语句使用Python中的cx_Oracle 例如,查询-从id位于“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,

我想在准备好的Oracle语句中使用IN子句,该语句使用Python中的cx_Oracle

例如,查询-从id位于“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不支持这个概念,而且您肯定不是第一个尝试这种方法的人!您必须:

为每个in值创建单独的绑定变量——这在Python中非常简单和直接 在Oracle类型上使用cast运算符创建子查询,如本文所示:

使用存储过程接受数组并直接在PL/SQL中执行多个查询

或者完全做别的事情


自从你创建了字符串,你就快到了。这应该起作用:

results = cursor.execute('select name from employee where id in ' + ALL_IDS)

在领事馆的会议上,组织一次行动

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()

query = """
select name from employee where id in ('{}')
""".format("','".join(map(str, ids)))

results = cursor.execute(query)
names = [x[0] for x in cursor.description]
rows = results.fetchall()


除非您将所有值列入白名单并进行验证,否则这将是一个重大的安全风险。如果您的数据未经清理,则此上的cx_Oracle文档可能是一个巨大的安全漏洞。通常必须使用绑定变量,请参阅。绑定变量还可以提高可伸缩性和性能,尽管如果查询执行得不多,或者变量的数量变化很大,可伸缩性的好处可能很小。