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
Solved! Go to Solution.
fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows: print row.getValue(fieldName)
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)
fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows: print row.getValue(fieldName)
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)
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.