arcpy.da.UpdateCursor on joined tables?

10438
25
Jump to solution
05-01-2013 06:05 AM
ErikMartin
Occasional Contributor
Do data access update cursors work on joined tables?  This thread indicates that Search Cursors do, but I keep getting a "cannot update join table" error.  I have confirmed that I am trying to update the original table, not the joined table.  Here's the pertinent code:

arcpy.MakeFeatureLayer_management(FC, "FC_lyr") FC_lyr = "FC_lyr" arcpy.AddJoin_management(FC_lyr, "joinField", joinedFCTable, "joinField")  fields =("{}.DataField".format(FC_lyr), "{}.OtherDataFiedl".format(joinedFCTable)) where = '"{}.OBJECTID" IS NOT NULL'.format(joinedFCTable)  with arcpy.da.UpdateCursor(FC_lyr, fields, where) as rows:     for row in rows:         row[0] = row[0] + row[1] 


As an aside, I am trying to use the da.UpdateCursor because it is far faster than using field the calculator (code that I already have working) & I am trying to optimize speed.

Thanks,
-Erik
Tags (2)
0 Kudos
25 Replies
ErikMartin
Occasional Contributor
Every value in the dict throws the same error... can't get it to work.
0 Kudos
ErikMartin
Occasional Contributor
Also tried
arcpy.AddMessage(str(joinDict[joinFieldValue][13]))

and
strJoinFieldValue = str(joinFieldValue)
arcpy.AddMessage((joinDict[strJoinFieldValue][13]))


to no avail...
0 Kudos
ChrisSnyder
Regular Contributor III
where it is working there are the same number of records in the update and join tables (one record in each table). Where it isn't working, the join table only has a few matches in the update table


What you need to do is to add some logic to deal with the situation where there are no matching keys in the lut, although I'm currious since you should be getting an error that says something to the effect of "key error - key not found". Like Matt said, I think you are getting issues with Null values (which in a cursor come out as a value of None.

So for example:

lutDict = dict([(r[0], (r[1], r[2])) for r in arcpy.da.SearchCursor(lutTbl, ["SOURCEJOINFIELD","SOURCEFIELD1","SOURCEFIELD2"])]) updateRows = arcpy.da.UpdateCursor(targetTbl, ["TARGETJOINFIELD","TARGETFIELD1","TARGFETFIELD2"]) for updateRow in updateRows:     joinFieldValue = updateRow[0]     if joinFieldValue in lutDict and joinFieldValue != None:                 updateRow[1] = updateRow[1] + lutDict[joinFieldValue][0]          updateRow[2] = updateRow[2] * lutDict[joinFieldValue][1]     else:         updateRow[1] = -9999         updateRow[2] = -9999     updateRows.updateRow(updateRow) del updateRow, updateRows
0 Kudos
ErikMartin
Occasional Contributor
Bingo!  That was the piece that was missing.  Again, many, many thanks to both of you!
0 Kudos
ChrisSnyder
Regular Contributor III
Awsome!

BTW: 90% of my Python-specific parlor tricks come from this single page: http://docs.python.org/2/tutorial/datastructures.html

Lists
Tuples
Sets
Dictionaries
List Comprehensions
Filter
Zip
etc.

It's all there...
ErikMartin
Occasional Contributor
Thanks, Chris... It really is cool how much you can do with Python data structures.  I have used dictionaries before, but in much simpler key:value pair format with simple int or string keys, not tuple keys.  The way we've been using them here brings it to a whole other level of power (and complexity!).

Thanks again.
0 Kudos