AnsweredAssumed Answered

Insert Cursor not working, even though exit code 0, on SDE Database

Question asked by MollyKFoley on Oct 18, 2017
Latest reply on Oct 19, 2017 by asrujit_pb

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:

#Set the workspace to access feature classes in the HbMonitoringTest database
arcpy.env.workspace = r'C:\Users\user\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog\myDbName_HbMonitoringTest.sde'

#Getting feature classes from database
PatchesFC = "Patches"

#Get input shapefile from user
InputFC = r'C:\Users\user\Desktop\GIS_Testing\HbtatTesting\ExpTestFC.shp'

#Set search cursor fields to iterate through the input feature class
scFields = ['SHAPE@','StrMPID', 'StateID', 'Point', 'PatchNum', 'IsDevelope',
            'CropTypeID', 'CropResidu', 'CnpyOver12', 'CnpyDecid',
            'CnpyConif', 'ShrubCover','ShbHiStems', 'GrassCover',
            'ForbCover', 'FrbAsProte', 'ForbSpecie', 'BareGround',
            'HerbHeight', 'IsQuailHab', 'ObsvDate']

#Set insert cursor fields to add new rows to Patches feature class
icFields = ['SHAPE@', 'MonitoringPointID', 'PatchID', 'StateID', 'Point', 'PatchNum',
            'IsDeveloped', 'CropTypeID', 'CropResidue', 'CnpyOver12', 'CnpyDecid',
            'CnpyConif', 'ShrubCover', 'ShbHiStemsDens', 'GrassCover', 'ForbCover',
            'FrbAsProtect', 'ForbSpecies', 'BareGround', 'HerbHeight', 'IsQuailHab',
            'ObsvDate', 'ObserverID']

#Instantiate empty list to add edited rows too
rows2Insert = []

#Create search cursor to pull data from the attribute table of the user-input shapefile
with arcpy.da.SearchCursor(InputFC, scFields) as sCursor:
    for row in sCursor:
        rowList = list(row)
        PatchID = '{' + str(uuid.uuid4()).upper() + '}'
        ObserverID = None
        rowList.insert(2, PatchID)
        rowList.append(ObserverID)
        rows2Insert.append(rowList)


#Create insert cursor to insert new rows into the Patches database
with arcpy.da.InsertCursor(PatchesFC, icFields) as iCursor:
    for item in rows2Insert:
        print item
        iCursor.insertRow(item)
        print "Row inserted."

 

This is the output I get in the output window upon running the script:

Outcomes