9

This is a simple process using the legacy cursors, but I cannot figure out how to do it with the newer Insert Cursor from the Data Access module. I basically want to take the entire row from a Search Cursor created on an SDE feature class in one database, and insert that row into an SDE feature class in another database using the Insert Cursor. The code below is how I am doing this using the legacy cursors, and it works quite well, but I would like to take advantage of the faster performance of the Data Access cursors. This runs on a list of feature classes that all have different numbers of fields (and different geometry types), though the schema for each feature class is identical between the databases (the databases are basically copies of one another):

    sourceAddRows = arcpy.SearchCursor(sourceFC)
    targetAddRows = arcpy.InsertCursor(targetFC)
    for sourceAddRow in sourceAddRows:
        targetAddRows.insertRow(sourceAddRow)

4 Answers 4

24

As long as your source and target FC's have the same number of fields and have the same geometry type, this should work:

# Get field objects from source FC
#
dsc = arcpy.Describe(sourceFC)
fields = dsc.fields

# List all field names except the OID field and geometry fields
# Replace 'Shape' with 'SHAPE@'
#
out_fields = [dsc.OIDFieldName, dsc.lengthFieldName, dsc.areaFieldName]
fieldnames = [field.name if field.name != 'Shape' else 'SHAPE@' for field in fields if field.name not in out_fields]


# Create cursors and insert new rows
#
with arcpy.da.SearchCursor(sourceFC,fieldnames) as sCur:
    with arcpy.da.InsertCursor(targetFC,fieldnames) as iCur:
        for row in sCur:
            iCur.insertRow(row)
8
  • Jason, thank you very much. Your answer makes perfect sense and should work. However, when trying it I get "SystemError: error return without exception set". No telling what is wrong as this error message tells us nothing.
    – jread
    Mar 20, 2014 at 23:35
  • 3
    Just thought of something: the destination feature class is not empty. Do you think that it's freaking out due to the OID field? If I'm copying all fields over, the OID would be included (I think?), and it may not like that since there is a possibility of duplicate OIDs being created. What do you think?
    – jread
    Mar 20, 2014 at 23:49
  • That sounds like a good possibility. I will edit the answer to omit the OID field. Mar 21, 2014 at 13:01
  • Just out of curiosity, why not use the CopyFeatures_management tool? Mar 21, 2014 at 13:49
  • Thanks, I will try the updated code sometime this weekend and let you know. The reason for not using CopyFeatures is that it is live SDE data being accessed (read-only) by users/apps. Using cursors allows me to push updates without worrying about locks, etc.
    – jread
    Mar 22, 2014 at 0:26
3

Google suggested this answer for the following arcpy.da.InsertCursor error: "SystemError: error return without exception set"

The answer suggested here does not solve the issue if your are trying to use the da.InsertCursor to add data in a Geometric Network.

There is currently a bug (Bug NIM102778) on file with ESRI over this issue. The solution suggested by ESRI is to insert the data into an empty feature class then append the feature class to the geometric network.

That solution does work, however it's much slower. The older version of InsertCursor arcpy.InsertCursor() will allow you to add data to a geometric network and maintain a decent level of performance.

2

This will create a Point FC from polygon centroids and keep two of the fields:

import arcpy,os

print "Script running..."

arcpy.env.overwriteOutput = 1
arcpy.env.workspace = r'C:\projects\Informationsdata_test.gdb\WGS84'

#Input polygon fc
infc = 'Bestand'
#Output point fc
outfc = 'Centrumpunkter'

fieldlist = ["Identifier","Lottning","SHAPE@XY"]

arcpy.CreateFeatureclass_management(out_path=arcpy.env.workspace, out_name=outfc, geometry_type='POINT')
arcpy.AddField_management(in_table=outfc, field_name=fieldlist[0], field_type='TEXT', field_length=20)
arcpy.AddField_management(in_table=outfc, field_name=fieldlist[1], field_type='TEXT', field_length=20)

with arcpy.da.SearchCursor(infc,fieldlist) as scur:
    with arcpy.da.InsertCursor(outfc, fieldlist) as icur:
        for row in scur:
            icur.insertRow(row)

print "Script complete

"

1

If you're simply iterating over the features in a SearchCursor and inserting into another table, are you making sure the destination feature class is empty prior to your script running?

Even with locks, the following code should work and doesn't rely on any cursors.

arcpy.DeleteFeatures_management(Destination_FeatureClass)
arcpy.Append_management(Source_FeatureClass, Destination_FeatureClass, "NO_TEST")

If this is a large volume of data, you can perform delta updates using the FME ChangeDetector or alternatively, script it up in Python using the Insert, Update, Delete cursors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.