1

I've created a function in my model to insert row in a table I've created. The problem is if it runs the InsertCursor function before the end of my program. My model executes incorrectly. I believe it's because the InsertCursor doesn't actually output anything. I get this message: "The process did not execute because the precondition is false." if I manually try to step through it. The message I get initially upon running model is: "All the inputs are not current." If I remove the precondition, it executes properly except it wants to run the function last, after it's already created the excel document that I'm trying to add the appended table to. I've already attempted this fix.

Here's the function:

import arcpy, inspect, os
fc = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "\ProposedDesignDB.mdb\AnodeEquipment"
fields = ['MATERIALNUMBER','MATERIALNUMBERTEXT']
def AnodeUpdate(y):
 cursor = arcpy.da.InsertCursor(fc, fields)
 for x in xrange (0, y):
 newRow = ["100212","ANODE 1 LB 3 FT LEAD 12 WHITE COPPER"]
 cursor.insertRow(newRow)
 del newRow
 del cursor

Has anyone else ever had this issue? enter image description here

asked Jan 20, 2017 at 15:53
0

2 Answers 2

2

In my experience, you MUST include the [OID] or [FID] or [OBJECTID] or whatever your table-type considers to be a {row_id} in your field list when using Insert Cursors (and Update Cursors). If this is contrary to any documentation you have read I apologize!

You may be asking " ... but why, the system generates the row_id automatically?"

True, but the argument passed to arcpy.da.InsertCursor.insertRow({arg}) should be a tuple, and that tuple must have a PLACE_HOLDER for the system generated value to be stored in.

I've uploaded examples using a dBase table and a stand-alone table from a Personal GeoDatabase. In both cases, omitting the {row_id} causes them to fail.

Map, Data, ToolBox Models @ dropbox

This won't work :

import arcpy, inspect, os
def gns_update():
 fc = "C:\\se_TimG\\test.dbf"
 fields = ['AGGREGATE']
 with arcpy.da.InsertCursor(fc, fields) as cursor:
 for x in range(1,5):
 cursor.insertRow( ("TESTING") )
 #End For
 #End With
 del cursor
 return

enter image description here

but THIS WILL work

import arcpy, inspect, os
def gns_update():
 fc = "C:\\se_TimG\\test_dbf.dbf"
 fields = ['OID','AGGREGATE']
 with arcpy.da.InsertCursor(fc, fields) as cursor:
 for x in range(1,5):
 cursor.insertRow( (763121, "TESTING") )
 cursor.insertRow( (8675309, "TESTING_2") )
 cursor.insertRow( (0, "TESTING_3") )
 #End For
 row_values = [ (0,"FROMTUPLE_LIST_1"), (0,"FROMTUPLE_LIST_2"), (0,"FROMTUPLE_LIST_3"), (0,"FROMTUPLE_LIST_4"), (0,"FROMTUPLE_LIST_5") ]
 for row in row_values:
 cursor.insertRow( row )
 #End For
 #End With
 del cursor
 return

enter image description here

The {row_id} value you pass in the tuple is irrelevant when INSERTING rows, what IS relevant is that an integer placeholder exists within the tuple. *The {row_id} value passed in the tuple has no bearing on the resultant system generated {row_id}. ie. the value you pass is ignored and a new one is created.

When performing Updates with an Update cursor, the {row_id} should match the row targeted for updating.

answered Oct 1, 2019 at 16:30
0

The solution was to create a separate model that just processed my line features. That way it ran the InsertCursor last, and then tied it back into my current model and ran normally.

answered Jan 22, 2017 at 13:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.