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
Hi Kamyaka Kasani ,
It looks like you are opening several thread that are related to the same issue? It is better to only post one, and if you have more information to help the original post, you can edit you post to add it (you can even change the title...but only if it help clarify...otherwise is can be confusing).
There are tips on posting https://community.esri.com/community/help-and-feedback/blog/2016/08/15/community-news-and-tips-how-to-ask-questions-and-get-answers?sr=search&searchId=55610eae-6c40-4925-9242-d5f4c4853a33&searchIndex=7 and any code can be posted using https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?sr=search&searchId=dd5239d4-49ff-4661-80b7-babe88e70b5e&searchIndex=6 This help every one know if spacing is correct (critical for Python) and be able to refer to the same line number. (see Xander Bakker code blocks vs your code blocks)
Just for reference, I think the other related posts are:
The requested operation is invalid on a closed state (#748417)
The requested operation is invalid on a closed state (#748418)
The requested operation is invalid on a closed state (#748387)
and this one which is post #748389
Having the relative responses in one post with ovoid duplication and will help others find a solution in the future. If there are no responses to some of the other posts, you can actually delete them...but only do so if there is no information, or if it is already included in another post. Thanks
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
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'
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
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit; font-size: 14px;">import</SPAN> arcpy <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit; font-size: 14px;">import</SPAN> os fc <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit; font-size: 14px;">=</SPAN> <SPAN class="" style="color: #669900; border: 0px; font-weight: inherit; font-size: 14px;">'Database Connections/Portland.sde/portland.jgp.schools'</SPAN> workspace <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit; font-size: 14px;">=</SPAN> os<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>path<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>dirname<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN>fc<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Start an edit session. Must provide the workspace.</SPAN> edit <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit; font-size: 14px;">=</SPAN> arcpy<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>da<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>Editor<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN>workspace<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Edit session is started without an undo/redo stack for versioned data</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># (for second argument, use False for unversioned data)</SPAN> edit<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>startEditing<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit; font-size: 14px;">False</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">,</SPAN> <SPAN class="" style="color: #990000; border: 0px; font-weight: inherit; font-size: 14px;">True</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Start an edit operation</SPAN> edit<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>startOperation<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Insert a row into the table.</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit; font-size: 14px;">with</SPAN> arcpy<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>da<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.UpdateCursor</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN>fc<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">,</SPAN> <SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN><SPAN class="" style="color: #669900; border: 0px; font-weight: inherit; font-size: 14px;">'Name'</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit; font-size: 14px;">as</SPAN> ucur<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">: </SPAN> for row in ucur: row[1] = '222' ucur.updateRow(row) # Failing at this case <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Stop the edit operation.</SPAN> edit<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>stopOperation<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit; font-size: 14px;"># Stop the edit session and save the changes</SPAN> edit<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">.</SPAN>stopEditing<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">(</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit; font-size: 14px;">True</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit; font-size: 14px;">)</SPAN>
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:
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os fc <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Database Connections/Portland.sde/portland.jgp.schools'</SPAN> workspace <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Start an edit session. Must provide the workspace.</SPAN> edit <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Editor<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Edit session is started without an undo/redo stack for versioned data</SPAN> <SPAN class="comment token"># (for second argument, use False for unversioned data)</SPAN> edit<SPAN class="punctuation token">.</SPAN>startEditing<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Start an edit operation</SPAN> edit<SPAN class="punctuation token">.</SPAN>startOperation<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Insert a row into the table.</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Name'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> icur<SPAN class="punctuation token">:</SPAN> icur<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">7642471.100</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">686465.725</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'New School'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Stop the edit operation.</SPAN> edit<SPAN class="punctuation token">.</SPAN>stopOperation<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Stop the edit session and save the changes</SPAN> edit<SPAN class="punctuation token">.</SPAN>stopEditing<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
... you can see there is a sequence of steps to be followed:
What kind of business logic will you be applying inside the cursor?
Here is my sample code
path = 'D:\\temp' connection = "test1.sde"
arcpy.CreateDatabaseConnection_management(path, connection, "ORACLE",instanse, "DATABASE_AUTH", "test",xyz, "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
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.
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?
Hi Xander,
Thanks for ur reply.
Yes, It is editing manually with the standard license.
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?
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.