Insert Cursor is failing after retriving value using search curor.

954
5
Jump to solution
12-13-2013 05:47 AM
JackAvis
New Contributor II
Hey GISers My code is failing to insert cursor value.  The value it is return for parcel is correct its just not inserting it into my table.  

A little help would be gratly appreciated.  Thank you.

CODE

# Insert Cursor for GDB table to GDB table 
# Import modules, environment settings
import arcpy, traceback, string
arcpy.env.overwriteOutput = True

# Database connection variables on SPGISPROC1
OutFGDB = r"P:\batch\APO\District_overlay.gdb\pa_fp_dat1"
InFGDB = r"P:\batch\APO\District_overlay.gdb\pa_fp_rel1"

#Create cursor for input table items
InCursor = arcpy.SearchCursor(InFGDB)

# Create a variable that stores the value for column in a given row of the input table

for InRow in InCursor:

    parcel      = InRow.getValue('pafp_frq_PARCEL') 
    fp_zone1    = InRow.getValue('pafp_frq_ZONE2')

    # Copy the stored value from the input table into the output table
    gdbCursor = arcpy.InsertCursor(OutFGDB)
    row = gdbCursor.newRow()

    if fp_zone1:
        fp_zone1 = fp_zone1.upper
       
    #Fill out fields with variables from input table       
    row.pafp_frq_PARCEL = parcel
    row.pafp_frq_ZONE2  = fp_zone1   

    #Insert new row into output file
    gdbCursor.insertRow(row)

#Delete cursor and row objects to remove locks on the data
del row
del cursor




ERROR Message

Traceback (most recent call last):
  File "C:\Python27\ArcGISx6410.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\gisdata\Python\Insert_table_Cursor.py", line 28, in <module>
    row.pafp_frq_PARCEL = parcel
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
RuntimeError: ERROR 999999: Error executing function.
>>>
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Pretty simple, I guess you're mixing up your field aliases with your actual field names --- and your answer was alluded to by Jason and from the last comment by GISer on GIS Stack Exchange here:
http://gis.stackexchange.com/questions/80428/insert-cursor-fails-to-write-results-to-table

I entered a few commands interactively - probably what you're asking is why the field doesn't exist?...that becomes clear when you use ListFields (which operates on the actual field names, not the field aliases, just as the cursors do) -- there's more code than you need below to identify your error as I did this in ArcMap's Python window (and I'd actually use the da search and insert cursors in the end if I were you).  See the 2nd ListFields which I guess you meant to target 'parcel' - so be careful mixing up names for fields.  Also I suggest you be more careful not to confuse variable names either, which you are dangerously close to doing with the name 'parcel'.
>>> infc = 'pa_fp_rel1' >>> outfc = 'pa_fp_dat1'  >>> InCursor = arcpy.SearchCursor(infc) >>> gdbCursor = arcpy.InsertCursor(outfc) >>> for InRow in InCursor: ...     parcel = InRow.getValue('pafp_frq_PARCEL') ...     fp_zone1 = InRow.getValue('pafp_frq_ZONE2') ...     row = gdbCursor.newRow() ...     if fp_zone1: ...         fp_zone1 = fp_zone1.upper() ...     row.pafp_frq_PARCEL = parcel ...     row.pafp_frq_ZONE2 = fp_zone1 ...     gdbCursor.insertRow(row) ...      Runtime error  Traceback (most recent call last):   File "<string>", line 7, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__     return setattr(self._arc_object, attr, ao) RuntimeError: ERROR 999999: Error executing function.   >>> fieldsIn = arcpy.ListFields(infc)  >>> for field in fieldsIn: ...     print field.name ...      OBJECTID pafp_frq_FREQUENCY pafp_frq_PARCEL pafp_frq_ZONE2 pafp_frq_Shape_Area pafp_frq2_OBJECTID pafp_frq2_FREQUENCY pafp_frq2_PARCEL pafp_frq2_Shape_Area  >>> fieldsOut = arcpy.ListFields(outfc) >>> for field in fieldsOut: ...     print field.name ...      OBJECTID parcel fp_zone1 >>> 

View solution in original post

0 Kudos
5 Replies
T__WayneWhitley
Frequent Contributor
I don't know if you've already noticed this, but you don't have to create the insert cursor in the loop - actually you can put the insert line under the search cursor line...otherwise you're looping and re-creating the cursor on every iteration, so it's probably slowing things down.

Check your data types on the fields too, source and destination...not sure if that's causing the error, but something to check.  On the actual line that it's reported to fail at, row.pafp_frq_PARCEL = parcel, I'd check the value of 'parcel' 1st, before 'forcing' it into the write table -- and it's not a bad idea either to be consistent with 'getValue' and 'setValue' (earlier in your script you used 'getValue' so why not stick with that convention and use 'setValue' as well?)

Wayne
0 Kudos
JackAvis
New Contributor II
Thanks Wayne,  I have moved the insert cursour outside the loop.  I have also double checked the field in the source and destination and they are present and of the same data type and length.  I Still have the same error.   Does it matter if there are addtional fields in the destination table and does the order matter.  Looking for other ideas.
0 Kudos
T__WayneWhitley
Frequent Contributor
Not sure without more error info -- does it fail on the very 1st iteration?
I'm not sure but the fact that your input field is named the same as your output field bothers me... check to see if you can write to a different field, if you don't mind.  There are certainly better ways to trace your error, but in a pinch I guess this will do.

Otherwise, I suggest you attach a zipped sample of your 2 tables (small samples, if necessary).

I'm heading out for now - have a great weekend.


Enjoy,
Wayne
0 Kudos
JackAvis
New Contributor II
Yes it's failing on the first iteration.   During debugging it's reading the first input valve from "pa_fp_rel1" but it still fails to write it to the ouput table "pa_fp_dat1"  The parcel values look fine, they are simple text fields in both tables.  I'm just not see where this is going bad.  I have attached a sample geodatabase with the both tables.  Your comments are greatly appreciated.
0 Kudos
T__WayneWhitley
Frequent Contributor
Pretty simple, I guess you're mixing up your field aliases with your actual field names --- and your answer was alluded to by Jason and from the last comment by GISer on GIS Stack Exchange here:
http://gis.stackexchange.com/questions/80428/insert-cursor-fails-to-write-results-to-table

I entered a few commands interactively - probably what you're asking is why the field doesn't exist?...that becomes clear when you use ListFields (which operates on the actual field names, not the field aliases, just as the cursors do) -- there's more code than you need below to identify your error as I did this in ArcMap's Python window (and I'd actually use the da search and insert cursors in the end if I were you).  See the 2nd ListFields which I guess you meant to target 'parcel' - so be careful mixing up names for fields.  Also I suggest you be more careful not to confuse variable names either, which you are dangerously close to doing with the name 'parcel'.
>>> infc = 'pa_fp_rel1' >>> outfc = 'pa_fp_dat1'  >>> InCursor = arcpy.SearchCursor(infc) >>> gdbCursor = arcpy.InsertCursor(outfc) >>> for InRow in InCursor: ...     parcel = InRow.getValue('pafp_frq_PARCEL') ...     fp_zone1 = InRow.getValue('pafp_frq_ZONE2') ...     row = gdbCursor.newRow() ...     if fp_zone1: ...         fp_zone1 = fp_zone1.upper() ...     row.pafp_frq_PARCEL = parcel ...     row.pafp_frq_ZONE2 = fp_zone1 ...     gdbCursor.insertRow(row) ...      Runtime error  Traceback (most recent call last):   File "<string>", line 7, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__     return setattr(self._arc_object, attr, ao) RuntimeError: ERROR 999999: Error executing function.   >>> fieldsIn = arcpy.ListFields(infc)  >>> for field in fieldsIn: ...     print field.name ...      OBJECTID pafp_frq_FREQUENCY pafp_frq_PARCEL pafp_frq_ZONE2 pafp_frq_Shape_Area pafp_frq2_OBJECTID pafp_frq2_FREQUENCY pafp_frq2_PARCEL pafp_frq2_Shape_Area  >>> fieldsOut = arcpy.ListFields(outfc) >>> for field in fieldsOut: ...     print field.name ...      OBJECTID parcel fp_zone1 >>> 
0 Kudos