ArcPy – Use arcpy.da.InsertCursor to Insert Entire Row from Search Cursor

arcpycursor

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)

Best Answer

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)
Related Question