5

I am getting an error when trying to use the arcpy.da.UpdateCursor(). The error I am receiving is as followed

The requested operation is invalid on a closed state

I am in the python windows and trying to work with the current document

upsql = "OBJECTID = 3456"
with arcpy.da.UpdateCursor("GravityMain",["FromManHole"],upsql) as upcurs:
     for updat in upcurs:
         if updat:
             updat[0] = '222'
             upcurs.updateRow(updat)
del upcurs  

How can I use the UpdateCursor with the current document mxd = arcpy.mapping.MapDocument("CURRENT")

I looked at arcpy.da.Editor but it wants the full path to the database.

Also, I am using a MSSQL database.


If I input the following I still get the error

fc = "C:\Users\name\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\Sewer.sde"
fcc = "C:\Users\name\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\Sewer.sde\EC_Sewer.DBO.Sewer_Network\EC_Sewer.DBO.ssGravityMain"
workspace = os.path.dirname(fc)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
upsql = "OBJECTID = 345"
with arcpy.da.UpdateCursor(fcc,["FromManHole"],upsql) as upcurs:
   for updat in upcurs:
      if updat:
            updat[0] = '222'
            upcurs.updateRow(updat)
edit.stopOperation()  
edit.stopEditing(True)          
del upcurs            
  • 2
    If you're using a versioned dataset, you'll need to start an edit session first using the arcpy.da.Editor class. – crmackey Feb 19 '16 at 3:12
  • The only version is the default version, which has no branches. Does that still count? – JotDot Feb 19 '16 at 3:26
  • 1
    yes, that is still a version – Midavalo Feb 19 '16 at 3:28
  • 1
    You need to use the path to the SDE database, not the feature dataset. – crmackey Feb 19 '16 at 4:22
  • 1
    @ed.hankins on a versioned database, which is what I have, you are going to have to use the arcpy.da.Editor() and you cannot start editing in ArcMap. When I tried without the arcpy.da.Editor(), I got the error. When I tried with arcpy.da.Editor() and I had an edited session started in ArcMap I get the error. The error went away and everything started working great when I got my path right and made sure I was not in edit mode on ArcMap. I had to use arcpy.da.Editor(). I had to make sure I was not in edit mode on ArcMap and get the right path since my feature class was in a feature dataset. – JotDot Feb 19 '16 at 16:21
2

First, I would suggest you modify your work flow to include looking up all the arcpy things you don't understand. That is part of the process of coding.

Here is the link to da.editor http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/editor.htm

Here is the link to da update cursor http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/updatecursor-class.htm

I have cobbled together how you would set up your script. You will need to switch out the variables etc.

Note how the path to the feature class and workspace are set up. An easy way to get the correct path syntax is to cut and paste the path from the catalog window, down in the database connections section.

Also note that you don't need to check for the existence of a row. If your where clause doesn't return any records, the cursor doesn't iterate.

And finally, note that you don't need to delete your cursor if you use a with. It happens automatically.

 import arcpy
 import os

 fc = 'Database Connections/Portland.sde/portland.jgp.roads'
 workspace = os.path.dirname(fc)
 fields = ['ROAD_TYPE', 'BUFFER_DISTANCE']


 # Start an edit session. Must provide the workspace.
 edit = arcpy.da.Editor(workspace)

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

 # Start an edit operation
 edit.startOperation()

 # Create update cursor for feature class 
 with arcpy.da.UpdateCursor(fc, fields) as cursor:
     # Update the field used in Buffer so the distance is based on road 
     # type. Road type is either 1, 2, 3 or 4. Distance is in meters. 
     for row in cursor:
         # Update the BUFFER_DISTANCE field to be 100 times the 
         # ROAD_TYPE field.
         row[1] = row[0] * 100
         cursor.updateRow(row) 

 # Stop the edit operation.
 edit.stopOperation()

 # Stop the edit session and save the changes
 edit.stopEditing(True)
  • Thanks for your comment. I seen what you posted on ESRI and if you look at what I have posted, you will see I already have the answer. The problem was that I needed to add the dataset path too since my feature class was inside of the dataset. I will be posting my answer / conclusion. Also thanks for pointing out about me checking for the existence of rows. You are correct and good catch. Thank you – JotDot Feb 19 '16 at 14:25
2

Here is my complete solution. I needed to use arcpy.da.editor and I needed to add the dataset along with the sde path in order to access the layers from the current working session/ document. This works great but I still need to do a few things but it is working. Also, make sure you are not in edit mode when using arcpy.da.Editor(). With that said, thanks for all the help.

fc = r"C:\Users\Darrick\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\Sewer.sde\EC_Sewer.DBO.Sewer_Network"

workspace = os.path.dirname(fc)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
upsql = "OBJECTID = 175204"

with arcpy.da.UpdateCursor("GravityMain",["FromManHole"],upsql) as upcurs:
    for updat in upcurs:
        updat[0] = '222'
        upcurs.updateRow(updat)

edit.stopOperation()
edit.stopEditing(True)
del edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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