2016-12-04 132 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)