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

2

Instead of using:

cursor = arcpy.da.InsertCursor(fc, fields)

use:

with arcpy.da.InsertCursor(fc, fields) as cursor:
    for newRow in cursor:

This method does not lock your data.

Therefore your code would look like:

import arcpy, inspect, os

fc = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "ProposedDesignDB.mdb\AnodeEquipment"
fields = ['MATERIALNUMBER','MATERIALNUMBERTEXT']

def AnodeUpdate(y):
 with arcpy.da.InsertCursor(fc, fields) as cursor:
     for x in xrange (0, y):
         for newRow in cursor:
             newRow[0] = "100212",
             newRow[1] = "ANODE 1 LB 3 FT LEAD 12 WHITE COPPER"
             cursor.insertRow(newRow)
 del newRow
 del cursor
  • When I run it this way I get: ERROR 000539 TypeError: 'da.InsertCursor' object is not iterable. – j.stanfield Jan 20 '17 at 16:19
  • Ive edited the code. this should work now. also, what is the precondition "y" for? i dont know why youre using a function, when there is no need to. – MacroZED Jan 20 '17 at 16:21
  • 1
    yeah, I still get that same error. Also, not sure if cursor.insertRow(Row) was supposed to be cursor.insertRow(newRow) – j.stanfield Jan 20 '17 at 16:28
  • Yes, i forgot to edit that. try that again. but i still dont think you need to use a function. the preconditon isnt being used. hence why youve got the error "precondition is false" – MacroZED Jan 20 '17 at 16:33
  • The precondition "y" is a value from another function, that calculates how many times I need to iterate this InsertCursor to append rows to a the Empty table "AnodeEquipment". – j.stanfield Jan 20 '17 at 16:33
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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