<?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 How to correct SQL expression for Search Cursor in Arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742605#M57399</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to create a search cursor to print out the values in an attribute table for a school shapefile. I need to print out all the values that correspond to HIGH SCHOOL in the field FACILITY for the School shapefile. For some reason the search cursor won't work when I try to apply the sql expression but works without it. I don't want to print out all the values in that field just specific ones. Is there something wrong with my sql statement? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#import arcpy module&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True &lt;/P&gt;&lt;P&gt;# I am setting the work path &lt;/P&gt;&lt;P&gt;env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'&lt;/P&gt;&lt;P&gt;#I will set the variable for the schools shapefile&lt;/P&gt;&lt;P&gt;schools = 'Schools'&lt;/P&gt;&lt;P&gt;#create search cursor to loop thru schools. use where clause to return schools&lt;/P&gt;&lt;P&gt;#that are high schools. Use loops&lt;/P&gt;&lt;P&gt;#print out all the highschool names&lt;/P&gt;&lt;P&gt;#print total no. of high school records&lt;/P&gt;&lt;P&gt;sr = arcpy.SpatialReference(4326) &lt;/P&gt;&lt;P&gt;#I will create a search cursor for the schools shapefile&lt;/P&gt;&lt;P&gt;field = "FACILITY"&lt;/P&gt;&lt;P&gt;exp1 = '"FACILITY" = "HIGH SCHOOL"'&lt;/P&gt;&lt;P&gt;cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(field))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 15 Aug 2015 03:11:40 GMT</pubDate>
    <dc:creator>fumaniwada1</dc:creator>
    <dc:date>2015-08-15T03:11:40Z</dc:date>
    <item>
      <title>How to correct SQL expression for Search Cursor in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742605#M57399</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to create a search cursor to print out the values in an attribute table for a school shapefile. I need to print out all the values that correspond to HIGH SCHOOL in the field FACILITY for the School shapefile. For some reason the search cursor won't work when I try to apply the sql expression but works without it. I don't want to print out all the values in that field just specific ones. Is there something wrong with my sql statement? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#import arcpy module&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True &lt;/P&gt;&lt;P&gt;# I am setting the work path &lt;/P&gt;&lt;P&gt;env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'&lt;/P&gt;&lt;P&gt;#I will set the variable for the schools shapefile&lt;/P&gt;&lt;P&gt;schools = 'Schools'&lt;/P&gt;&lt;P&gt;#create search cursor to loop thru schools. use where clause to return schools&lt;/P&gt;&lt;P&gt;#that are high schools. Use loops&lt;/P&gt;&lt;P&gt;#print out all the highschool names&lt;/P&gt;&lt;P&gt;#print total no. of high school records&lt;/P&gt;&lt;P&gt;sr = arcpy.SpatialReference(4326) &lt;/P&gt;&lt;P&gt;#I will create a search cursor for the schools shapefile&lt;/P&gt;&lt;P&gt;field = "FACILITY"&lt;/P&gt;&lt;P&gt;exp1 = '"FACILITY" = "HIGH SCHOOL"'&lt;/P&gt;&lt;P&gt;cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(field))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Aug 2015 03:11:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742605#M57399</guid>
      <dc:creator>fumaniwada1</dc:creator>
      <dc:date>2015-08-15T03:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct SQL expression for Search Cursor in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742606#M57400</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are a few issues:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;First of all, please use the instructions found here: &lt;A href="https://community.esri.com/migration-blogpost/1070" target="_blank"&gt;Posting Code blocks in the new GeoNet&lt;/A&gt;&amp;nbsp; for insert code into a thread&lt;/LI&gt;&lt;LI&gt;The code does not work since the where clause is not valid. The delimiters of a field depend on the type of data used (shapefile, personal- file or enterprise geodatabase). Use the AddFieldDelimiters to structure the where clause.&lt;/LI&gt;&lt;LI&gt;If the would have worked, the statement "&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;print(row.getValue(field))&lt;/SPAN&gt;" would have printed the value HIGH SCHOOL for each high school and not the name&lt;/LI&gt;&lt;LI&gt;In the code below on line 12 a list of all the HIGH SCHOOL names is made and on line 17 the length of that list is printed.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = r"S:\376\Summer15-2\ahutche1\lab07_data\SectionB\Schools.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fac = "FACILITY"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_name = "FieldNameWithHighSchoolNames" # specify the name of the field

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a valid where clause
&amp;nbsp;&amp;nbsp;&amp;nbsp; sql = "{0} = 'HIGH SCHOOL'".format(arcpy.AddFieldDelimiters(fld))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # print out all the highschool names
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst = [r[1] for r in arcpy.da.SearchCursor(fc, (fld_fac, fld_name), sql)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; for name in lst:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print name

&amp;nbsp;&amp;nbsp;&amp;nbsp; # print total no. of high school records
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "There are {0} High Schools".format(len(lst))

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code has not been tested and should be used with ArcGIS 10.1 SP1 or higher ...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:36:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742606#M57400</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T07:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct SQL expression for Search Cursor in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742607#M57401</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When doing SQL expressions for python code i usually start by creating the query in select by attribute and then copy it to the code this usually ensures i have the correct syntax&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Aug 2015 23:07:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742607#M57401</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2015-08-15T23:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct SQL expression for Search Cursor in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742608#M57402</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;On line 9, you have an udefined variable &lt;SPAN style="font-family: 'courier new', courier;"&gt;fld&lt;/SPAN&gt; in &lt;SPAN style="font-family: 'courier new', courier;"&gt;AddFieldDelimiters()&lt;/SPAN&gt;. It also gives&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;TypeError: AddFieldDelimiters() takes exactly 2 arguments (1 given)&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It needs a data source, then field name. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/addfielddelimiters.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/addfielddelimiters.htm"&gt;AddFieldDelimiters—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Aug 2015 16:43:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742608#M57402</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2015-08-17T16:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct SQL expression for Search Cursor in Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742609#M57403</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Good catch, thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Corrected code below (still not tested):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy, os
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = r"S:\376\Summer15-2\ahutche1\lab07_data\SectionB\Schools.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fac = "FACILITY"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_name = "FieldNameWithHighSchoolNames" # specify the name of the field
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_ws, fc_name = os.path.split(fc)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a valid where clause
&amp;nbsp;&amp;nbsp;&amp;nbsp; sql = "{0} = 'HIGH SCHOOL'".format(arcpy.AddFieldDelimiters(fc_ws, fld_fac))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # print out all the highschool names
&amp;nbsp;&amp;nbsp;&amp;nbsp; lst = [r[1] for r in arcpy.da.SearchCursor(fc, (fld_fac, fld_name), sql)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; for name in lst:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print name

&amp;nbsp;&amp;nbsp;&amp;nbsp; # print total no. of high school records
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "There are {0} High Schools".format(len(lst))

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;...edit: &lt;A href="https://community.esri.com/migrated-users/48550" target="_blank"&gt;Blake T&lt;/A&gt; detected another error in my code: the fld variable on line 10 is not set and should be replaced by fld_fac which holds the name of the FACILITY field...&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:36:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-correct-sql-expression-for-search-cursor-in/m-p/742609#M57403</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T07:36:20Z</dc:date>
    </item>
  </channel>
</rss>

