Cannot open workspace

3593
10
02-06-2018 04:39 AM
KamyakaKasani
New Contributor

Getting the error "cannot open workspace" while using arcpy.da.Editor(arcpy.env.workspace) for a versioned data 

Can anyone help on this.

Thanks,

Kamyaka 

0 Kudos
10 Replies
XanderBakker
Esri Esteemed Contributor

I assume that you have the right license level available to start the editing on that workspace? Is it a Enterprise GDB (would require Standard or up). When you manually start editing on that workspace, does that work?

0 Kudos
KamyakaKasani
New Contributor

Hi Xander,

  Thanks for ur reply.

  Yes, It is editing manually with the standard license.

Thanks,

Kamyaka 

0 Kudos
XanderBakker
Esri Esteemed Contributor

If you look at the second example down below on this page Editor—Help | ArcGIS Desktop you can see the sequence of commands you should use.  Can you post your code?

RebeccaStrauch__GISP
MVP Emeritus

And make sure you are specifying

Path to the workspace to edit. Editor can edit only one workspace at a time

and not the FC. That sometimes is something that is overlooked.  As Xander pointed out, check out the samples.

KamyakaKasani
New Contributor

Here is my sample code

path = 'D:\\temp'
connection = "test1.sde"

arcpy.CreateDatabaseConnection_management(path,
connection,
"ORACLE",
instanse,
"DATABASE_AUTH",
"test",
XXX,
"SAVE_USERNAME",
"",
database,
"TRANSACTIONAL",
<version_name>,"")

tablePath = path + "\\" + connection 

arcpy.env.workspace = tablePath

with arcpy.da.UpdateCursor(arcpy.env.workspace,[<field_name>], where) as cur:

     # updatecursor stuff come here 

0 Kudos
XanderBakker
Esri Esteemed Contributor

First of all, is this a script that will only be run 1 time or do you want to run it multiple times? If multiple times, it would be better to use an existing connection or check if it has already bee created. Creating a sde connection on the fly, requires additional like having the Oracle client 32 bits (ArcMap) and/or Oracle client 64 bits (ArcGIS Pro) installed and working and tnsnames.ora and sqlnet.ora configured properly (to name a few requirements).

If you look at the sample code that both Rebecca and I mentioned earlier:

import arcpy
import os

fc = 'Database Connections/Portland.sde/portland.jgp.schools'
workspace = os.path.dirname(fc)

# 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()

# Insert a row into the table.
with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur:
    icur.insertRow([(7642471.100, 686465.725), 'New School'])

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

... you can see there is a sequence of steps to be followed:

  1. you have you path to your feature class to be edited (line 4)
  2. you determine the "path" (sde connection) which will be your workspace (line 5)
  3. you create an editor object on your workspace (sde connection)  at line 8
  4. you start editing (see line 12) configuring that you want to edit versioned data
  5. your start the edit operation (line 15)
  6. on line 18 you start your update cursor with the logic you want to apply
  7. at the end you stop your edit operation (line 22)
  8. and finally you stop editing saving your edits (if they were successful) see line 25

What kind of business logic will you be applying inside the cursor?

0 Kudos
KamyakaKasani
New Contributor

I'm using Updatecursor logic all are working fine but while updating the row its getting failed at the statement 

RuntimeError: The requested operation is invalid on a closed state 

import arcpy
import os

fc = 'Database Connections/Portland.sde/portland.jgp.schools'
workspace = os.path.dirname(fc)

# 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()

# Insert a row into the table.
with arcpy.da.UpdateCursor(fc, ('Name')) as ucur:
    for row in ucur:
         row[1] = '222'
         ucur.updateRow(row)    # Failing at this case


# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)
0 Kudos
XanderBakker
Esri Esteemed Contributor

It should have failed on the line before also:

row[1] = '222'

This will not work since the index should be 0 and not 1. You only have 1 field.

and I don't think you have a featureclass and connection called like this:}

fc = 'Database Connections/Portland.sde/portland.jgp.schools'

0 Kudos
KamyakaKasani
New Contributor
Here is my code 
mxd = arcpy.mapping.MapDocument("CURRENT")
 for lyr in arcpy.mapping.ListLayers(mxd):
 if lyr.name == 'test':
 
    if lyr.supports("SERVICEPROPERTIES"):
       servProp = lyr.serviceProperties

       path = 'D:\\temp'
       connection = "ttene71.sde"
       fc = "test"
 
       where = 'OBJECTID = 1' #35585
       arcpy.CreateDatabaseConnection_management(path,connection,"ORACLE",<instance>,"DATABASE_AUTH",<user_name>,<password>,"SAVE_USERNAME","",<password>,"TRANSACTIONAL",<version_name>,"")
 
 
       dbc = 'Database Connections//test1.sde'
       workspace = os.path.dirname(dbc)
       editor = arcpy.da.Editor(workspace)
       editor.startEditing(False,True)
       editor.startOperation()
       cursor = arcpy.da.UpdateCursor(lyr,['OBJECTID','NAME'], where)
       for row in cursor:
  
           row[1] = '222' 
 
           cursor.updateRow(row) # getting the error here
       del row
       del cursor
       editor.stopOperation() 
       editor.stopEditing(True)
       del editor
  
 del mxd 
0 Kudos