Creating a Edit Function for SDE Editing?

2626
1
12-18-2015 09:59 AM
MatthewRusso
New Contributor II

This issue has come up time and time again.  I make programmatic edits in a version SDE.

To save everyone some time i have attached my code snippet that current works to update rows in SDE. (See Below)

db = database connection file
edit = arcpy.da.Editor(db)
edit.startEditing()
edit.startOperation()

with arcpy.da.UpdateCursor(fc, fields) as cursor:
     for row in cursor:
          for code in facilCodes:
               if code == row[0]:
               row[1] = "New Mexico Central"
               cursor.updateRow(row)
               count += 1
               print("This information")

edit.stopOperation()
edit.stopEditing(save_changes=True)

Formatted in this way, my code works... and updates the rows in the version SDE

What I am looking to do is write a class or a few functions so that I can reuse my edit code.  What I have tried so far is this...

def openEdit(database):
     edit = arcpy.da.Editor(database)
     edit.startEditing()
     edit.startOperation()

def closeEdit(database):
     edit = arcpy.da.Editor(database):
     edit.stopOperation()
     edit.stopEditing(save_changes=True):

# Then I run it like this:
db = database connection file
openEdit(db)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
     for row in cursor:
          for code in facilCodes:
               if code == row[0]:
               row[1] = "New Mexico Central"
               cursor.updateRow(row)
               count += 1
               print("This information")
closeEdit(db)

It gives me an error saying: RuntimeError: Objects in this class cannot be updated outside an edit session [GISUSER.T_SWITCH]

This leads me to believe after my openEdit function runs it does not leave the edit session open?

Any ideas to help me be able to reuse code like this?

1 Reply
RichardFairhurst
MVP Honored Contributor

I believe the Editor has to be passed in along with the database and passed back from each function.  The operation fails because you are re-initializing the arcpy.da.Editor instance rather than passing it back and forth from method to method so you are only managing one instance.  By re-initializing, the previous operation that was started in one method becomes invisible to the next and forgotten.  A cursor behaves much the same.  You could not start a cursor in one method and then call the cursor's updateRow operation in another method if you were not passing the original cursor between them.  If you start a new cursor in the second method it knows nothing about the cursor started in the original method and the two cursors do not cooperate as one cursor, they conflict as two cursors on the same workspace.  So instead try:

def openEdit(database, edit):  
    edit = arcpy.da.Editor(database)  
    edit.startEditing()  
    edit.startOperation() 

    return edit
  
def closeEdit(database, edit):  
    edit.stopOperation()  
    edit.stopEditing(save_changes=True): 

    return edit
  
# Then try running it like this:  
db = database connection file 
edit = None

edit = openEdit(db, edit)  

with arcpy.da.UpdateCursor(fc, fields) as cursor:  
    for row in cursor:  
          for code in facilCodes:  
              if code == row[0]:  
              row[1] = "New Mexico Central"  
              cursor.updateRow(row)  
              count += 1  
              print("This information"
edit = closeEdit(db, edit)