为什么在使用Oracle_cx时显示“无效长度”错误消息?

时间:2019-07-23 15:25:36

标签: python oracle cx-oracle bind-variables

我正在使用cx_Oracle,使用绑定变量'plat'和'tick'使用Python脚本执行sql命令。尝试执行此命令时,它给我错误消息“ ORA-24373:为语句指定了无效的长度”。

要进行调试,我使用与脚本相同的参数(plat = 1234567,tick ='ABCDE')通过Oracle(不是Python)进行了SQL调用,并按预期运行。我尝试将参数既作为字典又作为命名变量传递,但是两次都遇到相同的错误。

我尝试将值更改为较低的值(“ 1”和“ A”),但这甚至是“无效长度”。

updateRecords.py

import os
import cx_Oracle

# For security reasons I cannot show my 'create_connection()' function, 
# but suffice to say I have tested it and it works as desired.

...

#Setup:
WORKING_PATH = os.path.dirname(os.path.abspath(__file__))
SQL_PATH = os.path.join(WORKING_PATH, 'sql')
cnnSB = create_connection()
cnnSB_cursor = cnnSB.cursor()

...
fetchComp = open(os.path.join(SQL_PATH, 'fetchRecentEntry.sql'), 'r')
for x in range(0, 5):
    cnnSB_cursor.execute(fetchComp.read(), {"plat":'A', "tick":1}) # ERROR LINE

fetchRecentEntry.sql

select * 
from MFS_PCIINCEXTFUNDBYPLAT
where PLATFORM = :plat
and TICKER = :tick
and STARTDATE = (select max(STARTDATE) from MFS_PCIINCEXTFUNDBYPLAT
                 where PLATFORM = :plat
                 and TICKER = :tick)

以上代码段导致以下错误消息:

File "updateRecords.py", line 297, in main
    cnnSB_cursor.execute(fetchComp.read(), plat='A', tick=1)
cx_Oracle.DatabaseError: ORA-24373: invalid length specified for statement

我检查过的其他内容:

-我的fetchComp.read()返回所需的代码

-将变量作为dict对象传递不会更改错误消息

1 个答案:

答案 0 :(得分:0)

我找到了解决方法: 问题来自.read()在循环内被称为 。结果,它将第一次正确读取文件,但是在随后的循环中,它将仅读取null / EOF。

要修复,我要做的就是将sql.read()设置为循环之前 的变量,并使用该变量,而不是在每个循环中调用.read()。

示例:


sql = fetchComp.read()
    for index, testRow in testDF.iterrows():
        cnnSB_cursor.execute(sql, tick=testRow[1], plat=testRow[0])
        compDF = pd.DataFrame(cnnSB_cursor.fetchall())