首页 > 解决方案 > Python,Oracle_cx,使用查询结果作为参数列表进入循环插入语句

问题描述

在过去,我编写了一个 pl/sql 脚本,该脚本将表名和列名(表示来源)作为参数,然后对表中的所有列进行概要分析,给出有用的计数。

我目前正在自学python,并且正在以一种可以针对其他sql数据库执行的方式重写该pl/sql脚本,而不仅仅是oracle。所以我是 Python 新手。我正在浏览 Udemy 上的 Automate the Boring Stuff。目前我并不关心 sql 注入,因为我只是在学习 Python 语言。我省略了创建表语句以减少我粘贴的代码量。

该脚本在循环的第一遍插入正确的记录,但它不会启动第二个循环。这是 IDLE 输出,然后是代码。

================================================ RESTART: C:\Users\nathan\Documents\_work\_data_profiling_script\profiling_python_tester.py ================================================
('ETL_INS_DTM',)
insert into PROFILING_NWS6_PRT
            select 'PROFILING_NWS6', 'ETL_INS_DTM', SRCRECNO, count(*), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
            from PROFILING_NWS6
            group by SRCRECNO
            order by 1,2,3
executed
committed
**Traceback (most recent call last):
  File "C:\Users\nathan\Documents\_work\_data_profiling_script\profiling_python_tester.py", line 39, in <module>
    for row in cursor:
cx_Oracle.InterfaceError: not a query**

import cx_Oracle
conn = cx_Oracle.connect("system", "XXXX", "localhost/xe")
cursor = conn.cursor()

## parameter declaration
##########################################################################

# These 2 parameters populated by user
v_st = 'PROFILING_NWS6' # Source Table - table in which we are profiling the data
v_srcno = 'SRCRECNO' # Source Number - numeric column in v_st that identifies the source system

# These 3 parameters automatically populated
v_prt = v_st + '_PRT' # Profile Report Table - table name we want our report created as
v_log = v_st + '_LOG' # Log Table - script logging goes here, used for monitoring and debugging
v_top = v_st + '_TOP' # Top Table - temporary table to hold top 5 counts


# write script that populates Profile Report Table with rows for each source/column combination from source table
# these are required to join to when updating analysis fields
##########################################################################

sql = "Select column_name from user_tab_columns where table_name = '"+ v_st + "' and column_name <> '" + v_srcno + "'"
cursor.execute(sql)
for row in cursor:
    print(row)
    sql =   """insert into {x_prt}
            select '{x_st}', '{x_row}', {x_srcno}, count(*), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
            from {x_st}
            group by {x_srcno}
            order by 1,2,3""".format(x_prt = v_prt, x_srcno = v_srcno, x_st = v_st, x_row = row[0])
    print(sql)
    cursor.execute(sql)
    print('executed')
    cursor.execute('commit')
    print('committed')


#close connections
##########################################################################
cursor.close()
conn.close()

标签: pythonsqloraclecx-oracle

解决方案


这种编码架构将在 Python 和 DB 之间进行大量“往返”,因此远非最佳。小的改进包括使用connection.autocommit而不是完整的 SQL 提交(或connection.commit()调用)。然后你可以看看使用executemany()而不是多次execute()调用。总的来说,对于 Oracle,只需使用 PL/SQL 调用,因为这将只需要一次往返。


推荐阅读