I have a script that uses a search cursor on a shapefile to retrieve data, add a few things, puts each row in a list, and then copies each item in that list as a new row to a SDE feature class using an insert cursor. The problem is that when I use the insert cursor, it doesn't insert any rows into the SDE feature class, even though I get an exit code of 0 and no error messages. Based on the output window, it appears to have inserted the rows but if I go look at the feature class, nothing is there. I have tested the script by using an insert cursor on a regular file geodatabase feature class and it works just fine, so there must be something special about the SDE environment that I'm missing.
I have the appropriate data types and number of items to match to fields, so it is not a sequence size error or a data type issue. I have attempted to carry out the insert cursor action inside an editing session (edit.startEditing(False, False) and edit.stopEditing(True)) but no luck with that.
The data is not versioned and never will be. Could that be what is causing the issue? Is there something about database permissions that I don't understand and need to write into the code?
Script:
<SPAN class="comment token">#Set the workspace to access feature classes in the HbMonitoringTest database</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Users\user\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog\myDbName_HbMonitoringTest.sde'</SPAN>
<SPAN class="comment token">#Getting feature classes from database</SPAN>
PatchesFC <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Patches"</SPAN>
<SPAN class="comment token">#Get input shapefile from user</SPAN>
InputFC <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Users\user\Desktop\GIS_Testing\HbtatTesting\ExpTestFC.shp'</SPAN>
<SPAN class="comment token">#Set search cursor fields to iterate through the input feature class</SPAN>
scFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'StrMPID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'StateID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Point'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PatchNum'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'IsDevelope'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'CropTypeID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CropResidu'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CnpyOver12'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CnpyDecid'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'CnpyConif'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ShrubCover'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'ShbHiStems'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'GrassCover'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'ForbCover'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FrbAsProte'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ForbSpecie'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'BareGround'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'HerbHeight'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'IsQuailHab'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ObsvDate'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Set insert cursor fields to add new rows to Patches feature class</SPAN>
icFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MonitoringPointID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PatchID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'StateID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Point'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PatchNum'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'IsDeveloped'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CropTypeID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CropResidue'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CnpyOver12'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CnpyDecid'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'CnpyConif'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ShrubCover'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ShbHiStemsDens'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'GrassCover'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ForbCover'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'FrbAsProtect'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ForbSpecies'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'BareGround'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'HerbHeight'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'IsQuailHab'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'ObsvDate'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ObserverID'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Instantiate empty list to add edited rows too</SPAN>
rows2Insert <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Create search cursor to pull data from the attribute table of the user-input shapefile</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>InputFC<SPAN class="punctuation token">,</SPAN> scFields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> sCursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> sCursor<SPAN class="punctuation token">:</SPAN>
rowList <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
PatchID <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{'</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>uuid<SPAN class="punctuation token">.</SPAN>uuid4<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'}'</SPAN>
ObserverID <SPAN class="operator token">=</SPAN> None
rowList<SPAN class="punctuation token">.</SPAN>insert<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> PatchID<SPAN class="punctuation token">)</SPAN>
rowList<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ObserverID<SPAN class="punctuation token">)</SPAN>
rows2Insert<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>rowList<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Create insert cursor to insert new rows into the Patches database</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>PatchesFC<SPAN class="punctuation token">,</SPAN> icFields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> iCursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> rows2Insert<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> item
iCursor<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN>item<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Row inserted."</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></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>This is the output I get in the output window upon running the script:
