首页 > 解决方案 > Unique constraint violated oracle error. How can I get the insert statement of the violation in python?

问题描述

I have a code that looks like this:

sql1 = 'select column from table'
db1conn.execute(sql1) 
results = db1conn.fetchall()

sql2 = 'insert into table(column) values(:1)' 
db2conn.executemany(sql2,results)

sometimes for no known reason I get the 'Unique constraint violated' oracle error from python script. I would like python to tell me which insert statement failed when I did the executemany. how can I do this efficiently? I was thinking of preparing the sql2 statement myself using my own function. but that is probably not a good idea.

标签: pythoncx-oracle

解决方案


Take a look at the batch errors example from the cx_Oracle repository. That shows you how you can figure out which row caused the problem and how to use the batch errors feature so the executemany() call doesn't fail, but you still get to figure out which rows caused problems and what those problems were.


推荐阅读