过去,我编写了一个pl / sql脚本,该脚本以一个表名和一个列名(表示源)作为参数,然后分析表中的所有列以提供有用的计数。
我目前正在自学python,并且正在以可以针对其他sql数据库(而不仅仅是oracle)执行的方式重写pl / sql脚本。所以我是Python的新手。我正在对Udemy上的无聊的东西进行自动化。目前,我不关心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()
for row in cursor:
中的光标仍在使用,直到循环完成。在循环内执行cursor.execute(sql)
时,它将更改cursor
对象。因此,在第二个循环中,要迭代的cursor
项目是循环内commit
的光标。解决方案是在循环内创建或使用其他光标对象。
cursor = conn.cursor() # original cursor, as above
insert_cursor = conn.cursor() # new one for insert
sql = "Select column_name from user_tab_columns where table_name " # etc
for row in cursor.execute(sql):
print(row)
sql = """second sql""".format(...)
print(sql)
insert_cursor.execute(sql)
print('executed')
insert_cursor.execute('commit')
print('committed')
cursor.close()
insert_cursor.close()
conn.close()
此外,for row in cursor:
应为for row in cursor.fetchall():
。要不就for row in cur.execute(sql):
。