van*_*d39 5 python oracle cx-oracle prepared-statement
我想在 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()
Run Code Online (Sandbox Code Playgroud)
这不起作用。难道我做错了什么?
Oracle 不支持这个概念——而且您也绝对不是第一个尝试这种方法的人!您必须:
在 Oracle 类型上使用强制转换运算符创建子查询,如本文所示: https: //asktom.oracle.com/pls/asktom/f ?p=100:11:0::::p11_question_id:210612357425
使用存储过程接受数组并直接在 PL/SQL 中执行多个查询
或者完全做别的事情!