Is it possible to rollback an update made with an update cursor or an insert made with an insert cursor if an error occurs?
I need to go through a feature class and break numbers up into 100 groups. e.g.
num_start | num_end | other_value
---------------------------------
100 | 300 | foo
Needs to be:
num_start | num_end | other_value
---------------------------------
100 | 199 | foo
200 | 299 | foo
300 | 300 | foo
My update cursor changes the first row, and then the insert cursors inserts the remaining rows. The update and insert work fine, but when an error occurs the updated values remain causing the sequences to be incorrect, which of course means I need to re-export the feature class and start over again.
I am using Python 2.7.13.
Libraries: These are the libraries I import
import arcpy
from arcpy import env
import sys
import os
import logging
import time
from functools import partial # Allows function calls with parameters inside of dictionaries
What I tried: I though I could have a variable set before a try statement and then reset the data in an exception.
with arcpy.da.UpdateCursor(fc, "*") as updateCurs:
oldValue = [] # initialize empty value
for row in updateCurs:
oldValue = [rowVal for rowVal in row] # read row into array
try:
# do stuff
del updateCurs
except Exception, e:
# reset row to old row value
for i in range(0, len(oldValue)):
row[i] = oldValue[i] # set row values
# rollback
updateCurs.updateRow(oldValue)
The problem here is that it would only correct the last row updated, but not the all the previous ones before it.
Row attributes: all other row attributes like geometry are set using the initial row being expanded, which currently are all points.