Select to view content in your preferred language

SearchCursor sort an SDE table

1182
2
05-22-2012 08:19 AM
ScottMoyer
Deactivated User
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
Tags (2)
0 Kudos
2 Replies
ChrisSnyder
Honored Contributor
This should work:

rows_LDC = arcpy.SearchCursor(mapTable.dataSource, "INDEX_ = 'CPA' and TR = '" + str(TR_Select) + "'", "", "", "DATE_FIELD A")
0 Kudos
ScottMoyer
Deactivated User
Firstly, I cut and pasted the incorrect code examples in my original post. I mixed up the code blocks for CPA and LDC, the two values within the 'INDEX_' field of the table. Please disregard sentences beginning with "rows_CPA =..." and "Here's the expression: expression_CPA_select =...". Correct code is below.

Thank you Chris for your input. I got the code to run without error. Here's the line that works:
rows_LDC = arcpy.SearchCursor(mapTable.dataSource, expression_LDC_select, "", "", "DATE A")
When I used the suggested code it errored out as "TR_select" wasn't found. A while back I had a heck of a time getting the expression to work without error. Here's the block of working code:
# 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, "", "", "DATE A")
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 . . . ")

The 'DATE' field is a date field.

Here's the expresstion:
expression_LDC_select = " \"INDEX_\" = 'LDC' and \"TR\" = '%s' " % TR_select
0 Kudos