<?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: arcpy SQL expression for PostGIS in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649750#M50569</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The issue you are running into is less about database or Python requirements and more about Esri requirements.&amp;nbsp; For databases, single quotes are the predominant string delimiter although some database platforms do support using double quotes that way as well.&amp;nbsp; I have read in various places over time that the ANSI SQL standard itself defines double quotes for database object names delimiters and single quotes for string literals.&amp;nbsp; That said, I have never paid to get a copy of the ANSI SQL standard to verify that statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking at the Python documentation for string literals:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;A href="https://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow noopener noreferrer" target="_blank"&gt;2.4.1. String literals&lt;/A&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.&lt;BR /&gt;....&lt;BR /&gt;In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Outside of raw strings, the backslash is the primary way of escaping special characters like a single quote.&amp;nbsp; Triple quotes can be used, and are even preferred in some specific situations, but backslashes are most commonly used.&amp;nbsp; One interesting behavior with Python is that using one type of quotes to define a string literal means the other type of quotes are interpreted literally within the string:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; #using backslash to escape single quotes
&amp;gt;&amp;gt;&amp;gt; print 'Let\'s hear it for \'air quotes\'.'
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using triple quotes to escape single quotes
&amp;gt;&amp;gt;&amp;gt; print '''Let's hear it for 'air quotes'.'''
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using double quote string literal to allow single quotes
&amp;gt;&amp;gt;&amp;gt; print "Let's hear it for 'air quotes'."
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using single quote string literal with backslash to allow
&amp;gt;&amp;gt;&amp;gt; #double quotes
&amp;gt;&amp;gt;&amp;gt; print 'Let\'s hear it for "air quotes".'
Let's hear it for "air quotes".&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(&lt;EM&gt;The Jive syntax highlighting is creating an artifact on line 05&lt;/EM&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking at the Esri documentation for query expressions in ArcGIS:&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-size: 18pt;"&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/map/working-with-layers/building-a-query-expression.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Building a query expression&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 18pt;"&gt;Searching strings&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Strings must always be enclosed within single quotes. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;STATE_NAME = 'California'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Strings in expressions are case sensitive except when you're querying personal geodatabase feature classes and tables.&lt;BR /&gt;....&lt;BR /&gt;If the string contains a single quote you will first need to use another single quote as an escape character. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NAME = 'Alfie''s Trough'&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;When it comes to building string expressions in Python, for SQL or otherwise, I am a big advocate for using the Python string format method (&lt;A href="https://docs.python.org/2/library/stdtypes.html#str.format" rel="nofollow noopener noreferrer" target="_blank"&gt;str.format()&lt;/A&gt;).&amp;nbsp; The Python &lt;A href="https://docs.python.org/2/library/string.html#formatspec" rel="nofollow noopener noreferrer" target="_blank"&gt;Format Specification Mini-Language &lt;/A&gt;is very robust at building expressions, and I find it much more readable than string concatenation in most cases.&amp;nbsp; As long as the strings you are searching on don't have single or double quotes in them, the following code should work to generate a valid SQL expression for you:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;input_AZ = str(arcpy.GetParameterAsText(0))
sql_exp =&amp;nbsp; "aktenzahl = '{}'".format(input_AZ)
arcpy.SelectLayerByAttribute_management (lyr_postgis, "NEW_SELECTION", sql_exp)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the code above, I am using double quotes for the Python string literal so that I can use single quotes un-escaped to structure the query the way ArcGIS wants.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 03:33:39 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-12-12T03:33:39Z</dc:date>
    <item>
      <title>arcpy SQL expression for PostGIS</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649748#M50567</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am developing a toolbox where the user can select an attribute (dropdown list) which comes from a PostGIS database and based on this attribute a select statement (selectbyAttribute) will be executed... Sounds easy...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a code snippet:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_AZ = str(arcpy.GetParameterAsText(0))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql_exp =&amp;nbsp; 'aktenzahl = '&amp;nbsp; + input_AZ&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;&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; ("aktenzahl" is the column (character) of the PostGIS db)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management (lyr_postgis, "NEW_SELECTION", sql_exp)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sql_exp looks like this:&lt;/P&gt;&lt;P&gt;"aktenzahl = VIIa-20150429a"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but I get the following error:&lt;/P&gt;&lt;P&gt;ERROR 000358 &lt;/P&gt;&lt;P&gt;[The SQL statement was not a select statement]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does somebody know, how a SQL statement in arcpy has to look like (e.g. inverted comma) when executing on PostGIS?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;&lt;P&gt;Stefan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Apr 2015 10:00:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649748#M50567</guid>
      <dc:creator>StefanKaps1</dc:creator>
      <dc:date>2015-04-29T10:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy SQL expression for PostGIS</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649749#M50568</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you are querying a string value it will likely need to have single quotes (although I'm not entirely sure about PostGIS db requirements).&amp;nbsp; You could try to update your sql with the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="sql" __jive_macro_name="code" class="_jivemacro_uid_14303112428576621 jive_macro_code jive_text_macro" data-renderedposition="71_8_912_16" jivemacro_uid="_14303112428576621"&gt;&lt;P&gt;sql_exp =&amp;nbsp; """aktenzahl = '"""&amp;nbsp; + input_AZ + "'"&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which should produce:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;aktenzahl = 'VIIa-20150429a'&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Apr 2015 12:15:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649749#M50568</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2015-04-29T12:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy SQL expression for PostGIS</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649750#M50569</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The issue you are running into is less about database or Python requirements and more about Esri requirements.&amp;nbsp; For databases, single quotes are the predominant string delimiter although some database platforms do support using double quotes that way as well.&amp;nbsp; I have read in various places over time that the ANSI SQL standard itself defines double quotes for database object names delimiters and single quotes for string literals.&amp;nbsp; That said, I have never paid to get a copy of the ANSI SQL standard to verify that statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking at the Python documentation for string literals:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;A href="https://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow noopener noreferrer" target="_blank"&gt;2.4.1. String literals&lt;/A&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.&lt;BR /&gt;....&lt;BR /&gt;In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Outside of raw strings, the backslash is the primary way of escaping special characters like a single quote.&amp;nbsp; Triple quotes can be used, and are even preferred in some specific situations, but backslashes are most commonly used.&amp;nbsp; One interesting behavior with Python is that using one type of quotes to define a string literal means the other type of quotes are interpreted literally within the string:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; #using backslash to escape single quotes
&amp;gt;&amp;gt;&amp;gt; print 'Let\'s hear it for \'air quotes\'.'
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using triple quotes to escape single quotes
&amp;gt;&amp;gt;&amp;gt; print '''Let's hear it for 'air quotes'.'''
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using double quote string literal to allow single quotes
&amp;gt;&amp;gt;&amp;gt; print "Let's hear it for 'air quotes'."
Let's hear it for 'air quotes'.
&amp;gt;&amp;gt;&amp;gt; #using single quote string literal with backslash to allow
&amp;gt;&amp;gt;&amp;gt; #double quotes
&amp;gt;&amp;gt;&amp;gt; print 'Let\'s hear it for "air quotes".'
Let's hear it for "air quotes".&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(&lt;EM&gt;The Jive syntax highlighting is creating an artifact on line 05&lt;/EM&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking at the Esri documentation for query expressions in ArcGIS:&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-size: 18pt;"&gt;&lt;A href="http://desktop.arcgis.com/en/desktop/latest/map/working-with-layers/building-a-query-expression.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Building a query expression&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 18pt;"&gt;Searching strings&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Strings must always be enclosed within single quotes. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;STATE_NAME = 'California'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Strings in expressions are case sensitive except when you're querying personal geodatabase feature classes and tables.&lt;BR /&gt;....&lt;BR /&gt;If the string contains a single quote you will first need to use another single quote as an escape character. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NAME = 'Alfie''s Trough'&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;When it comes to building string expressions in Python, for SQL or otherwise, I am a big advocate for using the Python string format method (&lt;A href="https://docs.python.org/2/library/stdtypes.html#str.format" rel="nofollow noopener noreferrer" target="_blank"&gt;str.format()&lt;/A&gt;).&amp;nbsp; The Python &lt;A href="https://docs.python.org/2/library/string.html#formatspec" rel="nofollow noopener noreferrer" target="_blank"&gt;Format Specification Mini-Language &lt;/A&gt;is very robust at building expressions, and I find it much more readable than string concatenation in most cases.&amp;nbsp; As long as the strings you are searching on don't have single or double quotes in them, the following code should work to generate a valid SQL expression for you:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;input_AZ = str(arcpy.GetParameterAsText(0))
sql_exp =&amp;nbsp; "aktenzahl = '{}'".format(input_AZ)
arcpy.SelectLayerByAttribute_management (lyr_postgis, "NEW_SELECTION", sql_exp)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the code above, I am using double quotes for the Python string literal so that I can use single quotes un-escaped to structure the query the way ArcGIS wants.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:33:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649750#M50569</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T03:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy SQL expression for PostGIS</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649751#M50570</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joshua, I am impressed...That was exactly the problem and your explaination was outstanding!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you, you saved my day...&lt;/P&gt;&lt;P&gt;Stefan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 06:28:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649751#M50570</guid>
      <dc:creator>StefanKaps1</dc:creator>
      <dc:date>2015-04-30T06:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy SQL expression for PostGIS</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649752#M50571</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am glad you found the explanation helpful.&amp;nbsp; Building query expressions in ArcGIS using Python comes up from time to time in the forums, although the specific question usually varies.&amp;nbsp; I have been meaning to write a blog post about it, and your question gave me the chance to do a dry run.&amp;nbsp; Cheers.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Apr 2015 20:26:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-sql-expression-for-postgis/m-p/649752#M50571</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-04-30T20:26:46Z</dc:date>
    </item>
  </channel>
</rss>

