0

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

  • Thank you, but this answer didn't work for me. – j.stanfield Jan 20 '17 at 18:34
0

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
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.