arcpy.da.InsertCursor breaks after 1000 records

4977
9
Jump to solution
12-02-2015 12:44 AM
StabsstelleGIS
New Contributor III

Hi, I have a Pythonscript which transforms input featureclass and tables into several output tables and one featureclass. The output tables are connected to the featureclass and to each other with relationship classes (everything in one single GDB). For avoiding nested loops, i initially put the input data into pythonic lists ("data" in the code). I use ArcGIS 10.3.1, Python 2.7.8, here's the code (nothing special):

def insertRows(workspace, fields, data):
    edit.startOperation()
    with arcpy.da.InsertCursor(workspace, fields) as cursor:
        for row in data:
            try:
                cursor.insertRow(row)
            except:
                err = ""
                for elm in row:
                    err = err + str(elm) + "|"
                arcpy.AddError("Error at row: " + err)
                arcpy.AddError(sys.exc_info()[0])
    edit.stopOperation()

Stacktrace:

Traceback (most recent call last):
  File "XX.py", line 600, in <module>
    insertRows(xGDB + os.sep + "BP_Plan", fields_BP_Plan, dict_BP_Plan)
  File "XX.py", line 255, in insertRows
    cursor.insertRow(row)
RuntimeError: Feld kann nicht bearbeitet werden. # Field is not editable

Filling the tables with more than 8000 rows is no problem, but the code breaks exactly on inserting record #1000 into output featureclass. The row data itself has no problem (when I switch inputdata's OID sort direction, it also breaks on record #1000). The list objects are also filled. Maybe there are some known limitations on editsessions when many relationship classes are involved.

I would appreciate any hints.

gdi-hohenlohekreis.de
0 Kudos
1 Solution

Accepted Solutions
StabsstelleGIS
New Contributor III

hi, I just solved the problem by using the with statement before calling the function insertRows:

def insertRows(workspace, fields, data):
    #edit.startOperation()
    with arcpy.da.InsertCursor(workspace, fields) as cursor:
        for row in data:
            cursor.insertRow(row)
    del row
    del cursor
    #edit.stopOperation()

with arcpy.da.Editor(GDB) as edit:
    insertRows(GDB + os.sep + "fc1", fields_fc1, data_fc1)
    insertRows(GDB + os.sep + "table1", fields_table1, data_table1)
    insertRows(GDB + os.sep + "table2", fields_table2, data_table2)

it seems, that the construct with startEditing/startOperation is only working up to 1000 records.

Thanks for your help anyway

gdi-hohenlohekreis.de

View solution in original post

9 Replies
NeilAyres
MVP Alum

Is the edit object being passed to this function as well?

0 Kudos
StabsstelleGIS
New Contributor III

thanks for your hint but it's global

gdi-hohenlohekreis.de
0 Kudos
LukeSturtevant
Occasional Contributor III

This appears to be a limit for geodatabase tables. I just generated a geodatabase table and tried to insert 2000 random rows and it stopped at 1000, but when I did the same insert cursor on a dBase table not in a geodatabase all 2000 rows were inserted.

StabsstelleGIS
New Contributor III

hi, I just solved the problem by using the with statement before calling the function insertRows:

def insertRows(workspace, fields, data):
    #edit.startOperation()
    with arcpy.da.InsertCursor(workspace, fields) as cursor:
        for row in data:
            cursor.insertRow(row)
    del row
    del cursor
    #edit.stopOperation()

with arcpy.da.Editor(GDB) as edit:
    insertRows(GDB + os.sep + "fc1", fields_fc1, data_fc1)
    insertRows(GDB + os.sep + "table1", fields_table1, data_table1)
    insertRows(GDB + os.sep + "table2", fields_table2, data_table2)

it seems, that the construct with startEditing/startOperation is only working up to 1000 records.

Thanks for your help anyway

gdi-hohenlohekreis.de
BlakeTerhune
MVP Regular Contributor

You can probably get rid of the del cursor line because it's redundant with the with statement. The cursor will be deleted automatically when the with statement is exited; same with row.

StabsstelleGIS
New Contributor III

thanks, you are right del statements are needless when using with statement

gdi-hohenlohekreis.de
0 Kudos
NeilAyres
MVP Alum

I am sorry but I don't believe this explanation.

Inserts inside of an edit operation work for me.

Just did a test and added 10,000 rows. It could have been 10 million.

There is something else going on here.

StabsstelleGIS
New Contributor III

I don't know exactly if that's the crux, but it worked for me. Which version of ArcGIS / Python do you use?

gdi-hohenlohekreis.de
0 Kudos
NeilAyres
MVP Alum

I am on 10.3.1 & python 2.7.8

0 Kudos