首页 > 解决方案 > 无法在 cx_oracle 中“设置定义关闭”;

问题描述

我正在尝试从 python 执行一些 SQL 语句,但遇到了以下 SQL 行的问题:

设置定义关闭;

其他 SQL 语句不存在此类问题。

def Querydf(command_str, dsn_str, username, pw):  
    #function outputs data as a pandas dataframe
    conn = cx_Oracle.connect(user = username, password = pw, dsn=dsn_str)
    cursor = conn.cursor()

    cmd = command_str.split(";")
    if (len(cmd)>1):
        for i in cmd:
            temp = i.replace("\n","")
            print(temp)
            cursor.execute(temp)
    else:
        cursor.execute(command_str)

    Data = cursor.fetchall()
    col_names = []
    for i in range(0, len(cursor.description)):
        col_names.append(cursor.description[i][0])
    dataframe =  pd.DataFrame(data = Data, columns = col_names)
    cursor.close()
    conn.close()
    return dataframe

返回的错误是:cx_Oracle.DatabaseError: ORA-00922: missing or invalid option

如果有人能对此提供一些见解,那就太好了,谢谢!

标签: pythoncx-oracle

解决方案


“set define off”命令不是 SQL 语句,而是 SQL*Plus 语句。因此,它只能在 SQL*Plus 中执行。由于 cx_Oracle 只处理 SQL 语句,所以无论如何都不需要这个命令!如果您正在读取包含通常由 SQL*Plus 运行的 SQL 语句的文件,则需要过滤掉这些语句。


推荐阅读