[GIS] ArcPy.da.UpdateCursor error The requested operation is invalid on a closed state

arcpysql server

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            

Best Answer

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
Related Question