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?