2

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.

4
  • what have you tried? what was the result? how did it not meet your needs? what is your system like (which version of python, what other python/GIS libraries do you have access to)?
    – Paul H
    Mar 26, 2019 at 16:40
  • 1
    how are the geometries of the new rows determined?
    – Paul H
    Mar 26, 2019 at 16:42
  • What is the data target format? If enterprise geodatabase, which RDBMS? Have you declared a version or an edit session?
    – Vince
    Mar 26, 2019 at 18:21
  • @Vince thanks for your time, I just discovered I can use edit session right before your comment. For reference, data is within a personal geodatabase and no it is not versioned. There is the possibility it may move to an enterprise geodatabase in the future, in which case teh RDBMS would be SQL Server. What do you mean by data target format?
    – sushi
    Mar 26, 2019 at 18:39

1 Answer 1

3

I discovered I can use the edit session with Arcpy's Editor class. http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/editor.htm

# Create an edit sessions
edit = arcpy.da.Editor(arcpy.env.workspace)

# start edit session
# undo = enabled
# multiuser = False to allow full control over a nonversioned / versioned dataset
edit.startEditing(with_undo = True, multiuser_mode = False)

#start operations used in edit sessions
edit.startOperation()

with arcpy.da.UpdateCursor(midpointFileName, "*") as updateCurs, \
     arcpy.da.InsertCursor(midpointFileName, "*") as insertCurs:

    #do update stuff then delete cursor
    del updateCurs

    #do insert stuff then delete cursor
    del insertCurs

# commit all changes
edit.stopOperation()

# stop edit sessions
# True = Save changes
edit.stopEditing(True)

This was able to revert changes made with both update and insert cursors.

To address Vince's comment:

This data is kept in a file geodatabase.

Data is currently not versioned.

1
  • Please place question details in the Question. Versioning only applies to Enterprise geodatabase.
    – Vince
    Mar 26, 2019 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.