Select to view content in your preferred language

Proper arcpy.da.Editor use

25546
6
02-17-2015 04:32 PM
BlakeTerhune
MVP Regular Contributor

I'm on ArcGIS 10.2.2 and trying to use arcpy.da.Editor and an InserCursor on a versioned feature class. The feature class was registered without the option to move edits to base and resides in a 10.0 SDE on Oracle 11g (will be 10.2.2 SDE soon).

I found this helpful post by Leslie Morgan that really details my same situation, but there are no code samples. I would like to know if any of you have used the Python Editor in a similar setup and what your code looks like. More specifically, can the Editor class be used in a with statement like the Esri help article says or do I still have to start and stop the operation and edit session like users say?

Tags (1)
0 Kudos
6 Replies
BlakeTerhune
MVP Regular Contributor

Here is the basic way I'm using the Editor for now until I find a better way.

try:
    edit = arcpy.da.Editor(sdeconnection)
    print "edit created"
    edit.startEditing()
    print "edit started"
    edit.startOperation()
    print "operation started"
    # Perform edits
    with arcpy.da.InsertCursor(fc, fields) as fc_icursor:
        fc_icursor.insertRow(someNewRow)
    edit.stopOperation()
    print "operation stopped"
    edit.stopEditing(True)  ## Stop the edit session with True to save the changes
    print "edit stopped"
except Exception as err:
    print err
    if 'edit' in locals():
        if edit.isEditing:
            edit.stopOperation()
            print "operation stopped in except"
            edit.stopEditing(False)  ## Stop the edit session with False to abandon the changes
            print "edit stopped in except"
finally:
    # Cleanup
    arcpy.ClearWorkspaceCache_management()
BlakeTerhune
MVP Regular Contributor

Revisiting this, I've simplified the code a little by creating the edit session outside the try...except so you don't have to check if it exists.

edit = arcpy.da.Editor(sdeconnection)
print "edit created"
try:
    edit.startEditing()
    print "edit started"
    edit.startOperation()
    print "operation started"
    # Perform edits
    with arcpy.da.InsertCursor(fc, fields) as fc_icursor:
        fc_icursor.insertRow(someNewRow)
    edit.stopOperation()
    print "operation stopped"
    edit.stopEditing(True)  ## Stop the edit session with True to save the changes
    print "edit stopped"
except Exception as err:
    print err
    if edit.isEditing:
        edit.stopOperation()
        print "operation stopped in except"
        edit.stopEditing(False)  ## Stop the edit session with False to abandon the changes
        print "edit stopped in except"
finally:
    # Cleanup
    arcpy.ClearWorkspaceCache_management()
Jack_Zhang
Occasional Contributor

Thanks Blake, that's a lot better and clear than the example in ESRI's doc.

dbKlingdom
New Contributor II

Does anyone have a an ArcPro 3.0 solution on this??  We are using the code above but running arcpy.da.UpdateCursor in stead of  InsertCursor.  In debug on the arcpy.da.UpdateCursorline we get the error "cannot update the table". Thx

0 Kudos
by Anonymous User
Not applicable

Did you set the right booleans for your data?:

Both are defaulted to True, and the second parameter is should be False if your data isnt versioned.

startEditing ({with_undo}, {multiuser_mode})
# Edit session is started without an undo/redo stack for versioned data # (for second argument, use False for unversioned data) edit.startEditing(False, True)

 editor.htm 

0 Kudos
dbKlingdom
New Contributor II

Yes we are trying to write to an SQL SDE database.  We have set the edit to  edit.startEditing(False, True). Thx

0 Kudos