cx_oracle - 删除不工作。没有错误或例外

时间:2015-07-06 14:51:08

标签: python-3.x

我想写一个脚本来在for-while-loop-construct中运行几个SQL命令。到目前为止一切正常。除了删除。

脚本:

#!bin/python3.2
# script to remove batches of obsolete stuff from the tracking DB
#

import sys
import getpass
import platform
import cx_Oracle

# print some infos 
print("Python version")
print(("Python version: " + platform.python_version()))
print("cx_Oracle  version: " + cx_Oracle.version)
print("Oracle client: " + str(cx_Oracle.clientversion()).replace(', ','.'))

dbconn = cx_Oracle.connect('xxxx','yyyy', '1.2.3.4:1521/xxxRAC')
print ("Oracle DB version: " + dbconn.version)
print ("Oracle client encoding: " + dbconn.encoding)

cleanupAdTaKvpQuery = "delete from TABLE1 where TABLE2_ID < 320745354908598 and rownum <= 5"
getOldRowsQuery = "select count(*) from TABLE2 where ID < 320745354908598"

dbconn.begin()

cursor = dbconn.cursor() 
cursor.execute(getOldRowsQuery)
rowCnt = cursor.fetchall()

print("# rows (select before delete): " + str(rowCnt))

try:
    cursor.execute(cleanupAdTaKvpQuery)
    rows = cursor.rowcount
except: 
    print("Cleanup Failed.")

cursor.execute(getOldRowsQuery)
rowCnt = cursor.fetchall()

print("# rows (select after delete): " + str(rowCnt))

try: 
    dbconn.commit
    print("Success!")
except:
    print("Commit failed " + arg)

dbconn.close 

print("# of affected rows:" + str(rows))

正如您在输出中看到的那样。脚本运行正常,结果(请参阅rowCnt)有效且有意义,没有错误也没有异常,并且不会引发异常。

输出:

Python version
Python version: 3.2.3
cx_Oracle  version: 5.2
Oracle client: (11.2.0.3.0)
Oracle DB version: 11.2.0.3.0
Oracle client encoding: US-ASCII
# rows (select before delete): [(198865,)]
# rows (select after delete): [(198860,)]      <--- the result above decreased by 5!
Success!
# of rows:5
(ayemac_ora_cleanup)marcel@mw-ws:~/scripts/python/virt-envs/ayemac_ora_cleanup$ 

我错过了什么或做错了什么?我尝试用几个额外的select语句调试它,尝试捕获异常等等......

任何帮助表示赞赏!谢谢!

更新: 修复,感谢缺少括号的提示!

1 个答案:

答案 0 :(得分:1)

你缺少

中的括号
dbconn.commit()

没有它们,命令不会引发异常,而只是什么都不做。同样适用于dbconn.close()