如何在Python控制台中从动态SQL返回列

问题描述 投票:0回答:1

我正在尝试创建一个使用 Oracle 过程进行操作的 CRUD 应用程序。 对于 READ 组件,我编写了一个过程,根据指定的属性返回一个或多个整行。它在 Oracle SQL Developer 上工作正常,但一旦进入 Python,我就无法显示数据。

这是 Python 应用程序的 READ 部分:

def verEmpleado():
    opcionCorrecta = False
    while(not opcionCorrecta):
        print('================= CONSULTA =================')
        print('[1] Employee ID')
        print('[2] Employee name')
        print('[3] Job')
        print('[4] Manager')
        print('[5] Hire date')
        print('[6] Salary')
        print('[7] Commission')
        print('[8] Department ID')
        print('============================================')

        command = int(input('Seleccione la columna: '))
        match command:
            case 1:
                p_filter_att = 'empno'
                opcionCorrecta = True
            case 2:
                p_filter_att = 'ename'
                opcionCorrecta = True
            case 3:
                p_filter_att = 'job'
                opcionCorrecta = True
            case 4:
                p_filter_att = 'mgr'
                opcionCorrecta = True
            case 5:
                p_filter_att = 'hiredate'
                opcionCorrecta = True
            case 6:
                p_filter_att = 'sal'
                opcionCorrecta = True
            case 7:
                p_filter_att = 'comm'
                opcionCorrecta = True
            case 8:
                p_filter_att = 'deptno'
                opcionCorrecta = True
            case _:
                print('Comando incorrecto, intente de nuevo...\n')


    p_value_att = input('Ingrese el valor de la columna a seleccionar: ')

    data = [p_filter_att, p_value_att]
    return data

#CONNECT TO DB
def connVerEmpleado(data):
        try:
            conn = cx_Oracle.connect('HR/hr@localhost:1521/xepdb1')
        except Exception as err:
            print('Excepción al crear la conexión:', err)
        else:
            try:
                cursor = conn.cursor()
                cursor.callproc('query_emp', data)

                result = cursor.fetchall()
                print(result)
                cursor.close()

            except Exception as err:
                print('Error al conectar:', err)

这是程序

create or replace PROCEDURE query_emp(p_filter_att IN VARCHAR2, p_value_att IN VARCHAR2)
IS
    sql_qry              VARCHAR2(1000);
    TYPE bc_v_empno      IS TABLE OF NUMBER(4);
    v_empno              bc_v_empno;
    TYPE bc_v_ename      IS TABLE OF VARCHAR2(10);
    v_ename              bc_v_ename;
    TYPE bc_v_job        IS TABLE OF VARCHAR2(9);
    v_job                bc_v_job;
    TYPE bc_v_mgr        IS TABLE OF NUMBER(4);
    v_mgr                bc_v_mgr;
    TYPE bc_v_hiredate   IS TABLE OF DATE;
    v_hiredate           bc_v_hiredate;
    TYPE bc_v_sal        IS TABLE OF NUMBER(7,2);
    v_sal                bc_v_sal;
    TYPE bc_v_comm       IS TABLE OF NUMBER(7,2);
    v_comm               bc_v_comm;
    TYPE bc_v_deptno     IS TABLE OF NUMBER(2);
    v_deptno             bc_v_deptno;
BEGIN
    sql_qry := 'SELECT * FROM emp WHERE ' || p_filter_att || ' = UPPER(:p_value_att)';
    EXECUTE IMMEDIATE sql_qry BULK COLLECT INTO v_empno, v_ename, v_job, v_mgr, v_hiredate, v_sal, v_comm, v_deptno
    USING p_value_att;

    FOR i IN 1..v_empno.COUNT
        LOOP
            dbms_output.put_line('ID: ' || v_empno(i) || ' | NAME: ' || v_ename(i) || ' | JOB: ' || v_job(i) || ' | MANAGER: ' || v_mgr(i) || ' | HIRE DATE: ' || v_hiredate(i) || ' | SALARY: ' || v_sal(i) || ' | COMMISSION: ' || v_comm(i) || ' | DEPARTMENT: ' || v_deptno(i));
        END LOOP;

END query_emp;
/

当我使用 fetchall() 时,它返回错误而不是查询,其他方法根本不返回任何内容。

python oracle plsql dynamic-sql cx-oracle
1个回答
0
投票

有一个使用 DBMS_OUTPUT 的示例 https://github.com/oracle/python-oracledb/blob/main/samples/dbms_output.py.

但是,DBMS_OUTPUT 并不是最好的选择。 相反,只需返回类似于 https://github.com/oracle/python-oracledb/blob/main/samples/ref_cursor.py

的 REF CURSOR
© www.soinside.com 2019 - 2024. All rights reserved.