如何在 jaydebeapi.execute 中传递参数?

How to pass params in jaydebeapi .execute?

我想 select 使用 python 和 jaydebeapi 从 oracle DB 获取数据。我正在尝试使用下一个脚本获取数据:

bank_accounts = """
    SELECT ACCOUNT_NUM, VALID_TO, CLIENT, CREATE_DT, UPDATE_DT  
    FROM ACCOUNTS where client = ?"""

clients = ['6948','3603','3919']
curs.execute(bank_accounts, clients[0])

但结果我得到一个错误:

java.sql.SQLException: java.sql.SQLException: Invalid column index

这有什么问题吗?如何将参数传递给.execute?

在 Python 的 DB-API 规范 PEP 249 中,对于 cursor.execute,它指定 参数 参数 jaydebeapi 可坚持:

Parameters may be provided as sequence or mapping and will be bound to variables in the operation.

因此,当您尝试从列表中的第一项切片时,不要使用标量,而是考虑在元组或列表等序列中传递单个值:

# param as one-item tuple
curs.execute(bank_accounts, (clients[0],))

# params as one-item list
curs.execute(bank_accounts, [clients[0]])

或者,如果您对多个客户端使用了 IN 运算符,请直接传递您的列表:

bank_accounts = (
    "SELECT ACCOUNT_NUM, VALID_TO, CLIENT, CREATE_DT, UPDATE_DT "
    "FROM ACCOUNTS "
    "WHERE client IN (?, ?, ?)"
)

clients = ['6948','3603','3919']
curs.execute(bank_accounts, clients)