<?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: Passing a python variable to a (my) sql querie in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257880#M19847</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Randy- you're a genius!&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 04 Mar 2020 20:16:35 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2020-03-04T20:16:35Z</dc:date>
    <item>
      <title>Passing a python variable to a (my) sql querie</title>
      <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257878#M19845</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a series of functions that scrape data out of a number of tables in a MySQL db.&amp;nbsp; Everything to date has been working, but I need to add an enhancement.&amp;nbsp; Each of the tables has a date field that indicates the date of a particular permit that has been issued. For example, to get all the building permits for the year to date I can use:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;            query = """SELECT idProject, idController, siteAddress, 
                        siteApn,projectName, projectType,dateCreated,
                        datePermitIssued, permitNumber,applicationApprovedBy
                        FROM master_db.saltlakecountybuildingpermits
                        where datePermitIssued &amp;gt; '2019-12-31 23:59:59'
                        and siteAddress is not null;"""  &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;However, line 5 needs to be passed the date the script last ran.&amp;nbsp; I can capture that time stamp easily but I haven't been able to&amp;nbsp; figure out how to pass its value as a variable to the query. For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;ts = '2020-03-04 12:21:00'

           query = """SELECT idProject, idController, siteAddress, 
                        siteApn,projectName, projectType,dateCreated,
                        datePermitIssued, permitNumber,applicationApprovedBy
                        FROM master_db.saltlakecountybuildingpermits
                        where datePermitIssued &amp;gt; f'{ts}'
                        and siteAddress is not null;"""  &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;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;ts is the last time the script ran and what I am hoping for, but&amp;nbsp;no beuno (line 7 tosses an error). Once I put this into production, I only want to grab any records new since the last time the script was run, typically over night as a scheduled task.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:42:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257878#M19845</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-12-11T12:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a python variable to a (my) sql querie</title>
      <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257879#M19846</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does something like this work for you:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="operator token"&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;&amp;gt;&lt;/SPAN&gt; ts &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'2020-03-04 12:21:00'&lt;/SPAN&gt;
&lt;SPAN class="operator token"&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;&amp;gt;&lt;/SPAN&gt; query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""SELECT idProject, idController, siteAddress,
siteApn,projectName, projectType,dateCreated,
datePermitIssued, permitNumber,applicationApprovedBy
FROM master_db.saltlakecountybuildingpermits
where datePermitIssued &amp;gt; '{}'
and siteAddress is not null;"""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;ts&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="operator token"&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; query

SELECT idProject&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; idController&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; siteAddress&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
siteApn&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;projectName&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; projectType&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;dateCreated&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
datePermitIssued&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; permitNumber&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;applicationApprovedBy
FROM master_db&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;saltlakecountybuildingpermits
where datePermitIssued &lt;SPAN class="operator token"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'2020-03-04 12:21:00'&lt;/SPAN&gt;
&lt;SPAN class="operator token"&gt;and&lt;/SPAN&gt; siteAddress &lt;SPAN class="keyword token"&gt;is&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;not&lt;/SPAN&gt; null&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;/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;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:42:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257879#M19846</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-12-11T12:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a python variable to a (my) sql querie</title>
      <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257880#M19847</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Randy- you're a genius!&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Mar 2020 20:16:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257880#M19847</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-03-04T20:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a python variable to a (my) sql querie</title>
      <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257881#M19848</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think this also works for python3, put the "f" before the triple quotes ( not inside &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;ts &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'2020-03-04 12:21:00'&lt;/SPAN&gt;

query &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; f&lt;SPAN class="string token"&gt;"""SELECT idProject, idController, siteAddress,
siteApn,projectName, projectType,dateCreated,
datePermitIssued, permitNumber,applicationApprovedBy
FROM master_db.saltlakecountybuildingpermits
where datePermitIssued &amp;gt; '{ts}'
and siteAddress is not null;"""&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&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;/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;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:42:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257881#M19848</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-12-11T12:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a python variable to a (my) sql querie</title>
      <link>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257882#M19849</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sure does!&amp;nbsp; Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Mar 2020 20:41:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257882#M19849</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-03-04T20:41:04Z</dc:date>
    </item>
  </channel>
</rss>

