<?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 whereClause syntax error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73398#M6013</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script is almost complete.&amp;nbsp; I am attempting to have the user input the six values as strings, then the script selects and zooms to feature. I am running into a syntax error within the whereClause.&amp;nbsp; Any suggestions as to why?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Import import arcpy&amp;nbsp; #Variables&amp;nbsp; TOWNSHIP = arcpy.GetParameterAsText(0) RANGE = arcpy.GetParameterAsText(1) SEC = arcpy.GetParameterAsText(2) SUBCODE = arcpy.GetParameterAsText(3) BLOCK = arcpy.GetParameterAsText(4) LOT = arcpy.GetParameterAsText(5)&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]&amp;nbsp; #Logic try:&amp;nbsp; whereClause = ""TOWNSHIP" AND "RANGE" AND "SEC" AND "SUBCODE" AND "BLOCK" AND "LOT" = '"+Parcels+"'"&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)&amp;nbsp; df.extent = lyr.getSelectedExtent()&amp;nbsp; df.scale = df.scale*1.1&amp;nbsp; except:&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 26 Jul 2013 19:22:25 GMT</pubDate>
    <dc:creator>JacobDrvar</dc:creator>
    <dc:date>2013-07-26T19:22:25Z</dc:date>
    <item>
      <title>whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73398#M6013</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script is almost complete.&amp;nbsp; I am attempting to have the user input the six values as strings, then the script selects and zooms to feature. I am running into a syntax error within the whereClause.&amp;nbsp; Any suggestions as to why?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Import import arcpy&amp;nbsp; #Variables&amp;nbsp; TOWNSHIP = arcpy.GetParameterAsText(0) RANGE = arcpy.GetParameterAsText(1) SEC = arcpy.GetParameterAsText(2) SUBCODE = arcpy.GetParameterAsText(3) BLOCK = arcpy.GetParameterAsText(4) LOT = arcpy.GetParameterAsText(5)&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]&amp;nbsp; #Logic try:&amp;nbsp; whereClause = ""TOWNSHIP" AND "RANGE" AND "SEC" AND "SUBCODE" AND "BLOCK" AND "LOT" = '"+Parcels+"'"&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)&amp;nbsp; df.extent = lyr.getSelectedExtent()&amp;nbsp; df.scale = df.scale*1.1&amp;nbsp; except:&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 19:22:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73398#M6013</guid>
      <dc:creator>JacobDrvar</dc:creator>
      <dc:date>2013-07-26T19:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73399#M6014</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ands need to an individual equivalency check, you are scrimping on your coding.&amp;nbsp; Did you try one first?&amp;nbsp; then add a second?&amp;nbsp; equivalence is tested by == not = which is an assignment statement&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 19:43:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73399#M6014</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2013-07-26T19:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73400#M6015</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You said that the user is supposed to enter 6 values for the query?&amp;nbsp; Your query is not constructed correctly.&amp;nbsp; If you are wanting to a value for each variable you need to use something like "TOWNSHIP" = '81' AND "RANGE" = '04' etc.&amp;nbsp; Also, what is the value at the end (the 'Parcels') ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could do something like this and have the where clause get printed out in the results window for verification:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Import import arcpy, sys, traceback&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") &amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; fields = ["TOWNSHIP", "RANGE", "SEC", "SUBCODE", "BLOCK","LOT"] &amp;nbsp;&amp;nbsp;&amp;nbsp; sql_dict = {} &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql_dict[arcpy.AddFieldDelimiters(lyr, field)] = arcpy.GetParameterAsText(fields.index(field))&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # construct where clause &amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = ' AND '.join("{0} = '{1}'".format(k,v) for k,v in sql_dict.iteritems()) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('"&amp;nbsp; %s&amp;nbsp; "' %whereClause) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause) &amp;nbsp;&amp;nbsp;&amp;nbsp; df.extent = lyr.getSelectedExtent() &amp;nbsp;&amp;nbsp;&amp;nbsp; df.scale = df.scale*1.1&amp;nbsp; except: &amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the traceback object &amp;nbsp;&amp;nbsp;&amp;nbsp; tb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = sys.exc_info()[2] &amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback info:\n%s\nError Info:\n%s\n" % (tbinfo, sys.exc_info()[1]) &amp;nbsp;&amp;nbsp;&amp;nbsp; msgs&amp;nbsp; = "ArcPy ERRORS:\n%s\n" % arcpy.GetMessages(2) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(pymsg) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(msgs)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just did a quick test with some dummy variables, the where clause was:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;"&amp;nbsp; "SEC" = '36' AND "RANGE" = '04' AND "BLOCK" = '54' AND "SUBCODE" = 'NW NW' AND "TOWNSHIP" = '81' AND "LOT" = 'C'&amp;nbsp; "&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this the type of where clause you are trying to form?&amp;nbsp; Otherwise, I am confused as to what kind of expression you are trying to build.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 20:05:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73400#M6015</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-07-26T20:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73401#M6016</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the type of where clause I was attempting to create. It was just more advanced than I anticipated.&amp;nbsp; Can you explain the sql_dict, your whereClause statement, and the traceback?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Jul 2013 10:54:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73401#M6016</guid>
      <dc:creator>JacobDrvar</dc:creator>
      <dc:date>2013-07-29T10:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73402#M6017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the type of whereClause that I was attempting to build.&amp;nbsp; But, was much more involved than I anticipated.&amp;nbsp; Will you please explain the sql_dict, your whereClause statement, and how your traceback works?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Jul 2013 11:13:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73402#M6017</guid>
      <dc:creator>JacobDrvar</dc:creator>
      <dc:date>2013-07-29T11:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73403#M6018</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&lt;SPAN&gt;Sure.&amp;nbsp; What I did with the sql_dict was not necessary, it was just done for efficiency.&amp;nbsp; I built a python dictionary (you can read about these [url=&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://docs.python.org/2/library/stdtypes.html]here[/url" rel="nofollow noopener noreferrer" target="_blank"&gt;http://docs.python.org/2/library/stdtypes.html]here[/url&lt;/A&gt;&lt;SPAN&gt;])&amp;nbsp; Dictionaries store pairs known as keys and values.&amp;nbsp; So all I did was use the fields list and create a dictionary using each field as the key and grabs the parameter from the user using the arcpy.GetParameterAsText() for each user defined value for searching that field.&amp;nbsp; So for example, the dictionary will actually look like this:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the_dictionary[ key ] = value&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So to form a dictionary for your fields and the user defined value I made the key be the field, and the value is a parameter entered by the user.&amp;nbsp; Also note that I used the AddFieldDelimiters to grab the appropriate delimiter.&amp;nbsp; Maybe not necessary, but this way you can always be sure the field has the correct delimeter (personal gdb's use [], file gdb's use " " ). &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
sql_dict[arcpy.AddFieldDelimiters(lyr, field)] = arcpy.GetParameterAsText(fields.index(field))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Once the user has input parameters the actual dictionary will look like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
{"TOWNSHIP" : '81', "RANGE" : '04', "BLOCK" : '54', "SUBCODE" : 'NW NW', "SEC" : '36'}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So the above values corresponding to each field were values I entered using the tool and they are input into the dictionary because each value for each key is an "arcpy.GetParameterAsText()".&amp;nbsp; I filled in the correct index for the get params by finding the index of the field in the list (0-5).&amp;nbsp; I hope this makes sense.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for the where clause, I just iterated (using .iteritems() ) through the dictionary to grab each key and value pair (i.e. "SEC" = '36' ) using the .format() where the k,v just represents the key and value.&amp;nbsp; So for each pair you see in the above dictionary, it is just putting an equal sign between them.&amp;nbsp; Then, I used .join() to join each field and value in the dictionary by the ' AND ' operator to form the expression shown at the bottom of my last post.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The example I provided gets a little complicated, I suppose I was going for efficiency.&amp;nbsp; If you are just a beginner in Python you can always form the query in a way that makes more sense to you such as:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
TOWNSHIP = arcpy.GetParameterAsText(0)
RANGE = arcpy.GetParameterAsText(1)
SEC = arcpy.GetParameterAsText(2)
SUBCODE = arcpy.GetParameterAsText(3)
BLOCK = arcpy.GetParameterAsText(4)
LOT = arcpy.GetParameterAsText(5)

whereclause = ''' "TOWNSHIP" = '{0}' AND "RANGE" = '{1}' AND "SEC" = '{2}' AND "SUBCODE" = '{3}' AND "BLOCK" = '{4}' AND "LOT" = '{5}' '''.format(TOWNSHIP, RANGE, SEC, SUBCODE, BLOCK, LOT)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is essentially the exact same thing my dictionary and whereclause did, just with a little more typing.&amp;nbsp; But it may help you to understand how building the query works.&amp;nbsp; Using the .format() just inserts the user variables into the expression (from your arcpy.GetParameterAsText() ).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for the traceback, that is just a standard exception that Esri uses.&amp;nbsp; I think I copied that into a text file a long time ago from the help pages somewhere.&amp;nbsp; For some reason, sometimes the arcpy.GetMessages() doesn't work for me so in these cases I use that code to get all the exceptions (regular python errors and arcpy errors).&amp;nbsp; If your original except block was working, keep using that.&amp;nbsp; I sometimes use the whole Esri example to be safe.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:51:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73403#M6018</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-10T22:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73404#M6019</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for taking the time to help.&amp;nbsp; The explanations are extremely useful to a new python scipter, such as myself.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Jul 2013 12:38:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73404#M6019</guid>
      <dc:creator>JacobDrvar</dc:creator>
      <dc:date>2013-07-29T12:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73405#M6020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql_dict[arcpy.AddFieldDelimiters(lyr, field)] = arcpy.GetParameterAsText(fields.index(field))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # construct where clause
&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = ' AND '.join("{0} = '{1}'".format(k,v) for k,v in sql_dict.iteritems())

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had to give you a point for this one.&amp;nbsp; Great example (both syntax and useage) of using a dict AND join...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:51:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73405#M6020</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-10T22:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: whereClause syntax error</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73406#M6021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How can the script be altered to change Nulls(&amp;lt;Null&amp;gt;) to Blanks("")?&amp;nbsp; The script works well until a null is present in the attribute field.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fieldList = arcpy.ListFields(fc, "", "STRING")
 for field in fieldList:
&amp;nbsp; updateRows = arcpy.da.UpdateCursor(fc, RMS + " IS NULL", "", RMS)
 for updateRow in updateRows:
&amp;nbsp; updateRow.setValue(RMS, "")
&amp;nbsp; updateRows.updateRow(updateRow)
 del updateRow, updateRows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:51:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-syntax-error/m-p/73406#M6021</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2021-12-10T22:51:14Z</dc:date>
    </item>
  </channel>
</rss>

