我正在编写使用Python脚本执行SQL(Oracle)语句的代码,如下所示:
PYTHON:with open(os.path.join(WORKING_PATH, "sql", "fetchCalendar.sql"), 'r') as
fetch_date = fetch.read()
#(Some other code)
CD = timestamp_tostring(ACTUALDATE)
print("CD:", CD)
cnnSB.cursor.execute(fetch_date, CD)
fetchCalendar.sql
^{pr2}$
当我尝试使用以下值运行它时,将输出:CD: 30-AUG-18
Traceback (most recent call last): File "AHT_Init.py",
line 919, in
init_load(data, end_of_week, cur, 3) File "AHT_Init.py", line 644, in init_load
populate_rollup(modelid, test_data, train_data, rollup_cmd) File "AHT_Init.py", line 684, in populate_rollup
cnnSB.cursor.execute(fetch_date, CD) cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number
(强调'ORA-01036:非法变量名称/编号')
当然,我尝试更改变量名,但没有成功。我尝试使用Oracle SQL Developer运行SQL代码(在提示时传入'01-JUN-18',包括单引号),但得到以下错误:ORA-01858: a non-numeric character was found where a numeric was
expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
不过,当我用以下内容替换出有问题的行时:
^{3}$
代码按预期工作。
有谁能告诉我问题可能是什么,以及如何解决它吗?
非常感谢你!在
编辑:我知道在fetchCalendar.sql代码;这对于Python如何在这个上下文中与Oracle交互是必需的。出于测试目的,我在oraclesqldeveloper上尝试它时添加了一个分号,但是在为.SQL文件删除它时不会引起问题。在