Select to view content in your preferred language

Update cursor on joined tables?

5026
17
08-02-2010 08:06 AM
ChrisMathers
Deactivated User
Can we use an update cursor on a joined table in arcpy? I dont have 10 yet (very sad) because of a 3rd party plug-in conflict. I should have it soon so I am updating scripts for arcpy and this abilty would help me greatly.
0 Kudos
17 Replies
ChrisSnyder
Honored Contributor
Yes you can, and also can do this in pre-v10. But note that you can only alter field values in the input table (not the join table).
0 Kudos
ChrisMathers
Deactivated User
Really? How do you reference the field names ("name",[name],etc)? I have never had an update cursor work on a joined table before.
0 Kudos
ChrisSnyder
Honored Contributor
Something like:

inputTable = r"C:\temp\test.gdb\blah"
joinTable = r"C:\temp\test.gdb\lut"
gp.makefeaturelayer(inputTable, "FL")
gp.addjoin("FL","ID",joinTable,"ID")
updateRows = gp.updatecursor("FL")
updateRow = updateRows.next()
while updateRow:
   if updateRow.getvalue("lut.ANIMAL_TYPE") == "CAT":
       updateRow.FOOD_TYPE = updateRow.getvalue("lut.ANIMAL_TYPE") + " FOOD"
   updateRows.updaterow(updateRow)
   updateRow = updateRows.next()           
del updateRow
del updateRows
0 Kudos
ChrisSnyder
Honored Contributor
Something like:

inputTable = r"C:\temp\test.gdb\blah"
joinTable = r"C:\temp\test.gdb\lut"
gp.makefeaturelayer(inputTable, "FL")
gp.addjoin("FL","ID",joinTable,"ID")
updateRows = gp.updatecursor("FL")
updateRow = updateRows.next()
while updateRow:
   if updateRow.getvalue("lut.ANIMAL_TYPE") == "CAT":
       updateRow.FOOD_TYPE = updateRow.getvalue("lut.ANIMAL_TYPE") + " FOOD"
   updateRows.updaterow(updateRow)
   updateRow = updateRows.next()            
del updateRow
del updateRows
0 Kudos
ChrisMathers
Deactivated User
I cant create an update cursor on a joined table. This is the error given to me. I can create the cursor on an un-joined table though, but that isnt really the point.

import arcgisscripting
gp=arcgisscripting.create(9.3)
gp.makefeaturelayer_management(r"C:\GIS Projects\CRW\TrakIt_Source_RS.gdb\Target_Data\parce_test", "parce_test")
gp.addjoin("parce_test", "A1RENUM", r"C:\GIS Projects\CRW\parce_test_spatial_join.shp", "A1RENUM")
cursor=gp.updatecursor("parce_test")
row=cursor.Next()
while row:
    row.Flood_zn = row.getvalue("parce_test_spatial_join.FLD_ZONE")
    cursor.updaterow(row)
    row=cursor.Next()
del row
del cursor

Traceback (most recent call last):
  File "C:/GIS Projects/CRW/upgradetest", line 6, in <module>
    cursor=gp.updatecursor("parce_test")
RuntimeError: ERROR 999999: Error executing function.
0 Kudos
ChrisSnyder
Honored Contributor
I swear you used to be able to do this (v92?), but now that I actually need to do it in v9.3.1 SP1, I get the same error. I hate to choke on my own claims, but I'm pretty choked up right now! Dang!!!

runtimeError: ERROR 999999: Error executing function.

You certainly can search the joined records (in a searchcursor), as well as use the CalculateField tool (aka Update). Hmmm...
0 Kudos
ChrisMathers
Deactivated User
I've tried using JoinField and then using CalculateField. But with a large data set, as small as 100,000 records,this method takes way to long to be practical.
0 Kudos
deleted-user-rQoEFM5qzbHE
Deactivated User
I was getting this same error. I ended up having to export to a new shapefile and apply the UpdateCursor to this new shapefile.
0 Kudos
ChrisSnyder
Honored Contributor
I wonder if an updatecursor would work with a FeaterLayer or TableView created with the MakeQueryTable tool?
0 Kudos