2016-12-04 134 views
1

我希望将IN子句与使用Python中的cx_Oracle的预准备Oracle语句一起使用。用于Oracle的Oracle准备语句的IN子句cx_Oracle

E.g.查询 - select name from employee where id in ('101', '102', '103')

Python的一面,我有一个列表[101, 102, 103]我转换为一个字符串这样('101', '102', '103')和蟒蛇用下面的代码 -

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

这是行不通的。难道我做错了什么?

回答

2

这个概念不被Oracle支持 - 你绝对不是第一个尝试这种方法的人!你必须:

0

由于您创建了字符串,您几乎就在那里。这应该工作:

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