4

Can you access joined tables through a da.SearchCursor?

I've found documentation to do this with SearchCursor (arcpy), but nothing for the 10.1 data access module.

5

I realized that the join in ArcMap does not persist in an arcpy session. So I needed to create the join in arcpy before accessing the table with a SearchCursor. A simple example:

import arcpy
from sys import exc_info
from traceback import format_tb

exoticPath = r"...\Exotic_Species"
protocolsPath=r"...\Treatment_Protocols"

try:
    arcpy.MakeFeatureLayer_management(exoticPath,"exotic")
    arcpy.MakeTableView_management(protocolsPath,"protocols")
    arcpy.AddJoin_management("exotic","Species","protocols","CommonName")

    with arcpy.da.SearchCursor("exotic", ["Exotic_Species.Species","Treatment_Protocols.Priority"]) as cursor:
        for row in cursor:
            print (row[0], row[1])

except:
        tb = exc_info()[2]
        tbinfo = format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)
        print pymsg + "\n"
        print msgs


finally:
    if arcpy.Exists("exotic"):
        arcpy.Delete_management("exotic")
    if arcpy.Exists("protocols"):
        arcpy.Delete_management("protocols")
1

The arcpy.da.*Cursor are functionally equivalent to their arcpy.*Cursor counterpart... some additional niceties (context, more sql), and lots more speed. see doc for full details: http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000

When writing new code, always use arcpy.da.Cursors. If you have critical code that uses old cursors, you may want to switch that code to new cursors.

  • I do not think this is an answer to the question. – fatih_dur Feb 21 '18 at 4:30

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.