<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: if, else in searchcursor not working in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762921#M58943</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The first thing I would recommend is using more parameters in the search cursor to increase performance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;{Example}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SearchCursor (&lt;/SPAN&gt;&lt;STRONG&gt;dataset&lt;/STRONG&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;STRONG&gt;{where_clause}&lt;/STRONG&gt;&lt;SPAN&gt;, {spatial_reference},&lt;/SPAN&gt;&lt;STRONG&gt; {fields}&lt;/STRONG&gt;&lt;SPAN&gt;, {sort_fields})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed in your code you are building a query string and also search on a particular field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would try something like this to start;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 16 Aug 2012 18:40:32 GMT</pubDate>
    <dc:creator>KenCarrier</dc:creator>
    <dc:date>2012-08-16T18:40:32Z</dc:date>
    <item>
      <title>if, else in searchcursor not working</title>
      <link>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762920#M58942</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The following code always returns false, even when true, and the 'AddError' after the 'else' doesn't trigger.&amp;nbsp; When I comment out the 'else:' it works fine, when it's true.&amp;nbsp; The 'except:' doesn't trigger when it's false either?&amp;nbsp; Any thoughts?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Starting.....") &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(gisNumStr) &amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(stateName) &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("Name") == gisNumStr: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(gisNumStr) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(vLayer.name) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; vLayer.definitionQuery = query &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(query) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(vLayer, "NEW_SELECTION", query) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pdf.zoomToSelectedFeatures() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView ()&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(vLayer, "CLEAR_SELECTION") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView () &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Search completed..........") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd, pdf, vLayer, query, row, rows &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Item does not exist"); sys.exit() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows except: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("No");sys.exit()&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; del row &amp;nbsp;&amp;nbsp;&amp;nbsp; del rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 17:44:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762920#M58942</guid>
      <dc:creator>RobertEhrman</dc:creator>
      <dc:date>2012-08-16T17:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: if, else in searchcursor not working</title>
      <link>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762921#M58943</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The first thing I would recommend is using more parameters in the search cursor to increase performance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;{Example}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SearchCursor (&lt;/SPAN&gt;&lt;STRONG&gt;dataset&lt;/STRONG&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;STRONG&gt;{where_clause}&lt;/STRONG&gt;&lt;SPAN&gt;, {spatial_reference},&lt;/SPAN&gt;&lt;STRONG&gt; {fields}&lt;/STRONG&gt;&lt;SPAN&gt;, {sort_fields})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed in your code you are building a query string and also search on a particular field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would try something like this to start;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;fieldName = "Name" rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A") for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 18:40:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762921#M58943</guid>
      <dc:creator>KenCarrier</dc:creator>
      <dc:date>2012-08-16T18:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: if, else in searchcursor not working</title>
      <link>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762922#M58944</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, using more parameters is a good idea.&amp;nbsp; The following code worked, thanks to rfairhur24, aka Jalopena. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;hasVal = 0
for rows in arcpy.SearchCursor(fcName,query,None,fieldName):
&amp;nbsp;&amp;nbsp;&amp;nbsp; hasVal = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; break
del rows
if hasValue:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(Value + " Found")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # do some stuff here
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(Value + " Not found.....")&lt;/PRE&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The first thing I would recommend is using more parameters in the search cursor to increase performance.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;{Example}&lt;BR /&gt;SearchCursor (&lt;STRONG&gt;dataset&lt;/STRONG&gt;, &lt;STRONG&gt;{where_clause}&lt;/STRONG&gt;, {spatial_reference},&lt;STRONG&gt; {fields}&lt;/STRONG&gt;, {sort_fields})&lt;BR /&gt;&lt;BR /&gt;I noticed in your code you are building a query string and also search on a particular field.&lt;BR /&gt;&lt;BR /&gt;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&lt;BR /&gt;&lt;BR /&gt;I would try something like this to start;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
fieldName = "Name"
rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A")
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue(fieldName)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;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.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:25:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-in-searchcursor-not-working/m-p/762922#M58944</guid>
      <dc:creator>RobertEhrman</dc:creator>
      <dc:date>2021-12-12T08:25:31Z</dc:date>
    </item>
  </channel>
</rss>

