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)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
jread
  • 93
  • 1
  • 1
  • 3

4 Answers4

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)

jslatane
  • 167
  • 9
Jason Bellino
  • 4,321
  • 26
  • 35
  • 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 '14 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 '14 at 23:49
  • That sounds like a good possibility. I will edit the answer to omit the OID field. – Jason Bellino Mar 21 '14 at 13:01
  • Just out of curiosity, why not use the CopyFeatures_management tool? – Jason Bellino Mar 21 '14 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 '14 at 00:26
  • Well, your code works, but I still got the same cryptic error. Not sure what is going on but there must be something it doesn't like about the data. Going to mark this one as answered since I think you provided the solution, I just think there is something else going on here. Thanks again for your help! – jread Mar 22 '14 at 04:41
  • Great answer, and I might have been doing something wrong, but I had issues with 'SHAPE'. I had to remove it from fieldnames and replace it with 'SHAPE@'. – Richard Morgan Jan 16 '16 at 17:57
  • exclude geometry objects as well:
    out_fields = [dsc.OIDFieldName, dsc.lengthFieldName, dsc.areaFieldName]

    fieldnames = [field.name for field in fields if field.name not in out_fields]

    – dbusses Jun 02 '17 at 22:04
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.

Midavalo
  • 29,696
  • 10
  • 48
  • 104
wBowles
  • 37
  • 4
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

"

BERA
  • 72,339
  • 13
  • 72
  • 161
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.

Maltrap
  • 111
  • 1