if, else in searchcursor not working

1184
2
Jump to solution
08-16-2012 10:44 AM
RobertEhrman
New Contributor III
The following code always returns false, even when true, and the 'AddError' after the 'else' doesn't trigger.  When I comment out the 'else:' it works fine, when it's true.  The 'except:' doesn't trigger when it's false either?  Any thoughts? 

import arcpy, os, sys stateName = arcpy.GetParameterAsText(0) gisNum = arcpy.GetParameterAsText(1) gisNumStr = "g" + gisNum + ".tif" arcpy.AddMessage("Searching.....") query = "Name = " + "'" + "g" + gisNum + ".tif" + "'" gisName = ("'" + "g" + gisNum + ".tif" + "'") mxd = arcpy.mapping.MapDocument("CURRENT") pdf = arcpy.mapping.ListDataFrames(mxd)[0] vLayer = arcpy.mapping.Layer(stateName) arcpy.mapping.ListLayers(mxd, stateName) try:     arcpy.AddMessage("Starting.....")     arcpy.AddMessage(gisNumStr)     rows = arcpy.SearchCursor(stateName)     for row in rows:          if row.getValue("Name") == gisNumStr:                      arcpy.AddMessage(gisNumStr)             arcpy.AddMessage(vLayer.name)             vLayer.definitionQuery = query             arcpy.AddMessage(query)             arcpy.SelectLayerByAttribute_management(vLayer, "NEW_SELECTION", query)             pdf.zoomToSelectedFeatures()             arcpy.RefreshActiveView ()              arcpy.SelectLayerByAttribute_management(vLayer, "CLEAR_SELECTION")             arcpy.RefreshActiveView ()             arcpy.AddMessage("Search completed..........")             del mxd, pdf, vLayer, query, row, rows         else:             arcpy.AddError("Item does not exist"); sys.exit()         del row         del rows except:     arcpy.AddError("No");sys.exit()      del row     del rows
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenCarrier
Occasional Contributor III
The first thing I would recommend is using more parameters in the search cursor to increase performance.

I would start by hardcoding the values in a very simplistic cursor, once you have that working, I would try to build in the remainder of the logic.

{Example}
SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})

I noticed in your code you are building a query string and also search on a particular field.

To increase performance, I would not recommend doing a full table scan each time and searching for a field in a loop. I would limit the fields in the cursor itself which should prevent full table scans, also by specifying the where_clause in the cursor itself you should notice a performance gain

I would try something like this to start;

fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows:       print row.getValue(fieldName)


Esri's now recommends doing it this way at 10.1. Notice the {0} corresponds to fieldName and {1} to gisNum. This allows more flexibility in formatting strings rather than mixing double and single quotes with plus signs. A little cleaner as well.

query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"

fieldName = "Name" querystring = " UPPER(\"{0}\") = UPPER('g{1}.tif') ".format(fieldName,gisNum)) rows = arcpy.SearchCursor("yourlayername","where_clause=querystring, "",fieldName,"A") for row in rows:       print row.getValue(fieldName)


If you are at 10.1 look into arcpy.da.SearchCursor, it will outperform the 10.0 SearchCursor. At 10.0 there were performance issues with the SearchCursor especially when you have Subtypes or Domains in your data and if your data was in SDE.

View solution in original post

0 Kudos
2 Replies
KenCarrier
Occasional Contributor III
The first thing I would recommend is using more parameters in the search cursor to increase performance.

I would start by hardcoding the values in a very simplistic cursor, once you have that working, I would try to build in the remainder of the logic.

{Example}
SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})

I noticed in your code you are building a query string and also search on a particular field.

To increase performance, I would not recommend doing a full table scan each time and searching for a field in a loop. I would limit the fields in the cursor itself which should prevent full table scans, also by specifying the where_clause in the cursor itself you should notice a performance gain

I would try something like this to start;

fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows:       print row.getValue(fieldName)


Esri's now recommends doing it this way at 10.1. Notice the {0} corresponds to fieldName and {1} to gisNum. This allows more flexibility in formatting strings rather than mixing double and single quotes with plus signs. A little cleaner as well.

query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"

fieldName = "Name" querystring = " UPPER(\"{0}\") = UPPER('g{1}.tif') ".format(fieldName,gisNum)) rows = arcpy.SearchCursor("yourlayername","where_clause=querystring, "",fieldName,"A") for row in rows:       print row.getValue(fieldName)


If you are at 10.1 look into arcpy.da.SearchCursor, it will outperform the 10.0 SearchCursor. At 10.0 there were performance issues with the SearchCursor especially when you have Subtypes or Domains in your data and if your data was in SDE.
0 Kudos
RobertEhrman
New Contributor III
Yes, using more parameters is a good idea.  The following code worked, thanks to rfairhur24, aka Jalopena.
hasVal = 0
for rows in arcpy.SearchCursor(fcName,query,None,fieldName):
    hasVal = 1
    break
del rows
if hasValue:
    print(Value + " Found")
    # do some stuff here
else:
    print(Value + " Not found.....")

The first thing I would recommend is using more parameters in the search cursor to increase performance.

I would start by hardcoding the values in a very simplistic cursor, once you have that working, I would try to build in the remainder of the logic.

{Example}
SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})

I noticed in your code you are building a query string and also search on a particular field.

To increase performance, I would not recommend doing a full table scan each time and searching for a field in a loop. I would limit the fields in the cursor itself which should prevent full table scans, also by specifying the where_clause in the cursor itself you should notice a performance gain

I would try something like this to start;

fieldName = "Name"
rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A")
for row in rows:
      print row.getValue(fieldName)


Esri's now recommends doing it this way at 10.1. Notice the {0} corresponds to fieldName and {1} to gisNum. This allows more flexibility in formatting strings rather than mixing double and single quotes with plus signs. A little cleaner as well.

query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"

fieldName = "Name"
querystring = " UPPER(\"{0}\") = UPPER('g{1}.tif') ".format(fieldName,gisNum))
rows = arcpy.SearchCursor("yourlayername","where_clause=querystring, "",fieldName,"A")
for row in rows:
      print row.getValue(fieldName)


If you are at 10.1 look into arcpy.da.SearchCursor, it will outperform the 10.0 SearchCursor. At 10.0 there were performance issues with the SearchCursor especially when you have Subtypes or Domains in your data and if your data was in SDE.
0 Kudos