<?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 Long SQL Select Statements and Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/long-sql-select-statements-and-python/m-p/614738#M47968</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Earlier this week I posted &lt;A href="https://community.esri.com/thread/247006" target="_blank"&gt;Quoting Quotes&lt;/A&gt;&amp;nbsp; where I was dealing with a sql select statement.&amp;nbsp; As a follow up, here is something I learned today when dealing long variables.&amp;nbsp; I suspect a few of you guys (and you know who you are) already know this trick, but it's new to me so perhaps someone can benefit from it some time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dealing with (My) SQL Select statements in python became a challenge for me as I have several tables that have a ridiculous number of fields.&amp;nbsp; On top of that, some of them are free text 'comments' fields and those can be problematic because you never know what the contents are and what weird characters you will encounter that bomb things out at no charge.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A typical approach when&amp;nbsp;using python to scrape data from a SQL database is to open a cursor, and pass it a query.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;cn &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; mysql&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;connect&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;user &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'userName'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; password &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'password'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; host &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'yourHost'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; database &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'yourDatabase'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
cursor &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; cn&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;cursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SELECT some fields FROM yourDatabase.yourTable WHERE some condition is met;"&lt;/SPAN&gt;

‍‍‍‍‍‍‍‍‍‍‍cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;execute&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;query&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As mentioned I have a ton of fields to get and if I use:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SELECT * FROM dataBase.myTable"&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which selects ALL the fields with the * operator, I got myself in trouble when I encountered the weird characters in some of the fields.&amp;nbsp; What I ended up doing was entering groups of field names to my query variable until I found the one(s) that had bad characters in them. But, the select statement was so wide it is hardly readable in my Spyder ide code window.&amp;nbsp; Imagine listing 50 or 60 field names in one line.&amp;nbsp; Here is the solution:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""SELECT idProject, idController, siteAddress, siteApn,
            idRealProperty, projectName, projectType,
            projectValuation, numStories, totalSquareFootage, hasSepticSystem,
            hasIncludedDocs, isOverAcre, dwellingUnits, numWindows, roofingSquareFeet, 
            exteriorFinishType, finishingSquareFeet, newMeters, upgradedMeters, 
            tempMeters, additionalCircuits, numFurnaces, numACUnits, numHumidifiers, 
            numAirCleaners, numWaterHeaters, numWaterSofteners, numBoilers, 
            feetFencing, feetRetWall, kilowatts, footprintChange, septicAndBedrooms, 
            multipleKitchens, occupancyChange, roofSheath, licensedContractor, 
            ownerBuilder, affirmApplicant, certifyApplication, flags_0, flags_64,
            selected, activeSubRevCycles, gotoPhaseId, 
            reviewsComplete,reviews,paidDeposits, 
            paidFees, wasRejected, completedIssuances, dateCreated, applicationSubmitted, 
            applicationAccepted, lastSubmitted, submittalApproved, datePermitIssued
            FROM master_db.saltlakecountybuildingpermits;"""&lt;/SPAN&gt;  &lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is actually a trimmed down list of field names I needed but you get the idea. Notice how all the field names are comma and space separated and the last field name is not&amp;nbsp;covered by a comma.&amp;nbsp; The three quotes&amp;nbsp;at the beginning and end tells the interpreter to ignore the new lines and subsequently treats the variable value as one long string of characters.&amp;nbsp; I've never had to use this approach with a 'where' statement in an arcpy cursor, but I suspect it works there as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 02:17:31 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2021-12-12T02:17:31Z</dc:date>
    <item>
      <title>Long SQL Select Statements and Python</title>
      <link>https://community.esri.com/t5/python-questions/long-sql-select-statements-and-python/m-p/614738#M47968</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Earlier this week I posted &lt;A href="https://community.esri.com/thread/247006" target="_blank"&gt;Quoting Quotes&lt;/A&gt;&amp;nbsp; where I was dealing with a sql select statement.&amp;nbsp; As a follow up, here is something I learned today when dealing long variables.&amp;nbsp; I suspect a few of you guys (and you know who you are) already know this trick, but it's new to me so perhaps someone can benefit from it some time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dealing with (My) SQL Select statements in python became a challenge for me as I have several tables that have a ridiculous number of fields.&amp;nbsp; On top of that, some of them are free text 'comments' fields and those can be problematic because you never know what the contents are and what weird characters you will encounter that bomb things out at no charge.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A typical approach when&amp;nbsp;using python to scrape data from a SQL database is to open a cursor, and pass it a query.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;cn &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; mysql&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;connect&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;user &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'userName'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; password &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'password'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; host &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'yourHost'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; database &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'yourDatabase'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
cursor &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; cn&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;cursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SELECT some fields FROM yourDatabase.yourTable WHERE some condition is met;"&lt;/SPAN&gt;

‍‍‍‍‍‍‍‍‍‍‍cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;execute&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;query&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As mentioned I have a ton of fields to get and if I use:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SELECT * FROM dataBase.myTable"&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which selects ALL the fields with the * operator, I got myself in trouble when I encountered the weird characters in some of the fields.&amp;nbsp; What I ended up doing was entering groups of field names to my query variable until I found the one(s) that had bad characters in them. But, the select statement was so wide it is hardly readable in my Spyder ide code window.&amp;nbsp; Imagine listing 50 or 60 field names in one line.&amp;nbsp; Here is the solution:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""SELECT idProject, idController, siteAddress, siteApn,
            idRealProperty, projectName, projectType,
            projectValuation, numStories, totalSquareFootage, hasSepticSystem,
            hasIncludedDocs, isOverAcre, dwellingUnits, numWindows, roofingSquareFeet, 
            exteriorFinishType, finishingSquareFeet, newMeters, upgradedMeters, 
            tempMeters, additionalCircuits, numFurnaces, numACUnits, numHumidifiers, 
            numAirCleaners, numWaterHeaters, numWaterSofteners, numBoilers, 
            feetFencing, feetRetWall, kilowatts, footprintChange, septicAndBedrooms, 
            multipleKitchens, occupancyChange, roofSheath, licensedContractor, 
            ownerBuilder, affirmApplicant, certifyApplication, flags_0, flags_64,
            selected, activeSubRevCycles, gotoPhaseId, 
            reviewsComplete,reviews,paidDeposits, 
            paidFees, wasRejected, completedIssuances, dateCreated, applicationSubmitted, 
            applicationAccepted, lastSubmitted, submittalApproved, datePermitIssued
            FROM master_db.saltlakecountybuildingpermits;"""&lt;/SPAN&gt;  &lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is actually a trimmed down list of field names I needed but you get the idea. Notice how all the field names are comma and space separated and the last field name is not&amp;nbsp;covered by a comma.&amp;nbsp; The three quotes&amp;nbsp;at the beginning and end tells the interpreter to ignore the new lines and subsequently treats the variable value as one long string of characters.&amp;nbsp; I've never had to use this approach with a 'where' statement in an arcpy cursor, but I suspect it works there as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:17:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/long-sql-select-statements-and-python/m-p/614738#M47968</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-12-12T02:17:31Z</dc:date>
    </item>
    <item>
      <title>Re: Long SQL Select Statements and Python</title>
      <link>https://community.esri.com/t5/python-questions/long-sql-select-statements-and-python/m-p/614739#M47969</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Multiline strings with the triple quotes is a common method for this type of thing. Typically, triple double quotes &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;"""content"""&lt;/SPAN&gt; is used for &lt;A href="https://www.python.org/dev/peps/pep-0008/#string-quotes" rel="nofollow noopener noreferrer" target="_blank"&gt;docstrings&lt;/A&gt; that describe the use of functions so I use triple single quotes &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;'''contnent'''&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively, you could use join and format string methods with a list of field names.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"one"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"two"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"three"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"four"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
sql &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"select {} from table_name"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;","&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Going one step further, you can dynamically get the list of fields with &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/listfields.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy.ListFields()&lt;/A&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;f&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; f &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListFields&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;r&lt;SPAN class="string token"&gt;"path\to\my\data"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
sql &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"select {} from table_name"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;","&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:17:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/long-sql-select-statements-and-python/m-p/614739#M47969</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-12-12T02:17:34Z</dc:date>
    </item>
  </channel>
</rss>

