Trying to edit a layer in a versioned SDE GDB in a Python stand-alone script

6171
10
Jump to solution
07-17-2013 12:47 PM
JaimeMcKeown
New Contributor II
Hello everyone,

I am trying to do a simple edit using Calculate field on a layer in a versioned SDE GDB. I've gotten past a few errors, but now have ran into a new one that I can seem to get past. The error is "Objects in this class cannot be updated outside an edit session". I'm trying to use arcpy.da.Editor, but I seem to be missing something.  Any ideas would be greatly appreciated!

fc = "Database Connections\\CCENTGIS_SCLFinal_PRSCL_test.sde\\prscl_test.SCLADMIN.GMSCL\\prscl_test.SCLADMIN.sclarc" workspace = "Database Connections\\CCENTGIS_SCLFinal_PRSCL_test.sde"  arcpy.MakeFeatureLayer_management(fc, "sclarc_layer")  edit = arcpy.da.Editor(workspace)  edit.startEditing() edit.startOperation()  with arcpy.da.Editor(workspace) as edit:     arcpy.CalculateField_management("sclarc_layer", "STRNAME", "!strname!.strip", "PYTHON_9.3", "")  edit.stopOperation() edit.stopEditing(True) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Can you post your code you are running again? The only lines you should need are these

import arcpy workspace = "Database Connections\\CCENTGIS_SCLFinal_PRSCL_test.sde\\prscl_test.SCLADMIN.GMSCL" fc = "prscl_test.SCLADMIN.sclarc" arcpy.env.workspace = workspace arcpy.MakeFeatureLayer_management(fc, "sclarc_layer") with arcpy.da.Editor(workspace) as edit:     arcpy.CalculateField_management("sclarc_layer", "STRNAME", "!strname!.strip", "PYTHON_9.3", "")

View solution in original post

0 Kudos
10 Replies
MathewCoyle
Frequent Contributor
I believe you need to set your workspace to your feature dataset, not the general database.
0 Kudos
JaimeMcKeown
New Contributor II
When I set the workspace to the dataset I get "runtime error: cannot open workspace".
0 Kudos
MathewCoyle
Frequent Contributor
Are you connected with a version user that can make edits?
0 Kudos
JaimeMcKeown
New Contributor II
I'm trying to edit a test version of the database, and it turns out it wasn't setup properly.  Now that's fixed and I can edit, but it still won't run properly.

This is my latest error:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "G:\prscl\appl\Python\Scl_qcarcs_test.py", line 20, in <module>
    edit.stopOperation()
AttributeError: 'Workspace Operation object' object has no attribute 'stopOperation'
0 Kudos
MathewCoyle
Frequent Contributor
Ah yes you'll have to have those inside your with statement as afterwards your edit variable is closed automatically.
0 Kudos
JaimeMcKeown
New Contributor II
I moved edit.stopOperation() and edit.stopEditing() into the statement and I still get the same error.  I'm beginning to think this isn't going to work...
0 Kudos
MathewCoyle
Frequent Contributor
Have you tried removing those lines completely? The help examples shows just the with statement as able to stop and close the edit session automatically.

http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000005000000

    with arcpy.da.Editor(workspace) as edit:
        arcpy.CalculateField_management(
            layer_name, 'Usage', '"PUBLIC"', 'PYTHON')
0 Kudos
JaimeMcKeown
New Contributor II
I did and it still doesn't work, very frustrating...
0 Kudos
MathewCoyle
Frequent Contributor
Can you post your code you are running again? The only lines you should need are these

import arcpy workspace = "Database Connections\\CCENTGIS_SCLFinal_PRSCL_test.sde\\prscl_test.SCLADMIN.GMSCL" fc = "prscl_test.SCLADMIN.sclarc" arcpy.env.workspace = workspace arcpy.MakeFeatureLayer_management(fc, "sclarc_layer") with arcpy.da.Editor(workspace) as edit:     arcpy.CalculateField_management("sclarc_layer", "STRNAME", "!strname!.strip", "PYTHON_9.3", "")
0 Kudos