geomanscott

SearchCursor sort an SDE table

Discussion created by geomanscott on May 22, 2012
Latest reply on May 23, 2012 by geomanscott
Greetings GIS Pros,

Goal: Help needed in creating proper Python syntax for sorting a SearchCursor on a stand-alone table in an enterprise SDE. (Similar issues in working with UpdateCursor can be helpful.)

Background: A Python script is run in an MXD that is grabbing text from a stand-alone table in an enterprise SDE and putting the text into text elements of the the layout view. The SearchCursor is selecting text based on the DataDrivenPage index variable (in this case Township Range). The table is added as a source in the MXD.

This line of the script works properly (without sorting):
rows_CPA = arcpy.SearchCursor(mapTable.dataSource, expression_CPA_select)
Here's the expression:
expression_CPA_select = " \"INDEX_\" = 'CPA' and \"TR\" = '%s' " % TR_select . This is searching in the 'INDEX' field for rows with the 'CPA' value and also searching in the 'TR" field for the DataDrivenPage variable.
The working code selects and inserts the text as it should, but it comes in sorted by OID, not chonologically as desired (more on dates to follow).
Arc"Help" shows syntax as:
SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})
And an example:
rows = arcpy.SearchCursor("C:/Data/Counties.shp", "", "", "NAME; STATE_NAME; POP2000", "STATE_NAME A; POP2000 D")
Expanding the working code into the suggested syntax doesn't work, nor with other character combinations \ [ { * % (maybe the correct one was missed?). A possible issue here is that a stand-alone table doesn't have a spatial index. Sorting by a date would be ideal, but the 'sort_fields' part must be a string (this isn't a major holdup, syntax is).

Here's the block of code where this incident occurs (sorry, I don't know how to get it into a separate window):
# Put selected cells of LDC table data into mxd text elements ??? check for null value
if LDC_count > 0:
line_LDC = 1
line_LDC_str = str(line_LDC)
rows_LDC = arcpy.SearchCursor(mapTable.dataSource, expression_LDC_select)
for row_LDC in rows_LDC:
  Num_LDC = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Num_LDC_" + line_LDC_str)[0]
  Num_LDC.text = row_LDC.NUMBER
  Desc_LDC = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Desc_LDC_" + line_LDC_str)[0]
  Desc_LDC.text = row_LDC.DESCRIPTION
  MapDate_LDC = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapDate_LDC_" + line_LDC_str)[0]
  MapDate_LDC.text = row_LDC.MAPDATE
  ORD_LDC = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ORD_LDC_" + line_LDC_str)[0]
  ORD_LDC.text = row_LDC.ORDINANCE
  line_LDC = line_LDC + 1
  line_LDC_str = str(line_LDC)
else:
arcpy.AddMessage("No LDC Text ??? Moving On . . . ")

Thank you for reading this far. All knowledgable help is welcomed. Also, similar issues in working with UpdateCursor can be helpful.

Scott

Outcomes