<?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: Finding multiple tables and setting them to a variable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521543#M40888</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Mike,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not too great with Oracle, but it looks like sqllr2 is getting ignored. Maybe this means you are not fetching the data you want and the for loop is running on an empty list. Would it be better to get the results from both sql statements combined?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp; ...
&amp;nbsp;&amp;nbsp; sqllr1 = "select * from cat where table_name like '" +str(LRName1)+"%'"
&amp;nbsp;&amp;nbsp; sqllr2 = "select * from cat where table_name like '" +str(LRName)+"%'"
&amp;nbsp;&amp;nbsp; sql=sqllr1+" UNION "+sqllr2
&amp;nbsp;&amp;nbsp; curs.execute(sql)

&amp;nbsp;&amp;nbsp; tables = curs.fetchall()
&amp;nbsp;&amp;nbsp; ...&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:44:57 GMT</pubDate>
    <dc:creator>JoshuaChisholm</dc:creator>
    <dc:date>2021-12-11T22:44:57Z</dc:date>
    <item>
      <title>Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521542#M40887</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is something that just isn't right with this and I can figure it out. Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;nbsp;&amp;nbsp; import sys, string, os, cx_Oracle, arcpy, datetime

&amp;nbsp;&amp;nbsp; LRName = "RSID_"+num
&amp;nbsp;&amp;nbsp; LRName1 = "RSID"+num
&amp;nbsp;&amp;nbsp; last_year = str(now.year -1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; Two_years = str(now.year -2)

&amp;nbsp;&amp;nbsp; orcl = cx_Oracle.connect('xxxxx/xxxxx@xxx')
&amp;nbsp;&amp;nbsp; curs = orcl.cursor()
&amp;nbsp;&amp;nbsp; sqllr1 = "select * from cat where table_name like '" +str(LRName1)+"%'"
&amp;nbsp;&amp;nbsp; sqllr2 = "select * from cat where table_name like '" +str(LRName)+"%'"
&amp;nbsp;&amp;nbsp; curs.execute(sqllr1)

&amp;nbsp;&amp;nbsp; tables = curs.fetchall()
&amp;nbsp;&amp;nbsp; for table in tables:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last_year in table:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LastRun_Table = table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif Two_years in table:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LastRun = table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs.execute(sqllr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tables1 = curs.fetchall()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for table1 in tables1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last_year in table1:
&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; LastRun_Table = table1
&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; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif Two_years in table1:
&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; LastRun_Table = table1
&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; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&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; LastRun_Table = ""

&amp;nbsp;&amp;nbsp; curs.close()
&amp;nbsp;&amp;nbsp; orcl.close()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically I am trying to look for certain tables from previous years that match with what I am running now, set that table name to a variable that I will use later. This runs through but I am pretty sure that it is not going through the "for" statement. If I put in "fetchone" instead of "fetchall", it will go through the script once and than I get this for an error: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;TypeError: 'NoneType' object is not iterable&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is just a small part of a large script that I am trying to get to work. The large script loops through several feature classes and within each feature class it does this portion (plus other things). I am kind of confused on the ".fetch" thing but it seem to be the only way that I could put the table name into a variable. I would think that there is an easier way to do this but I don't know. Any help would appreciated. Thanks in advance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 21:10:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521542#M40887</guid>
      <dc:creator>MikePowell</dc:creator>
      <dc:date>2014-03-12T21:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521543#M40888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Mike,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not too great with Oracle, but it looks like sqllr2 is getting ignored. Maybe this means you are not fetching the data you want and the for loop is running on an empty list. Would it be better to get the results from both sql statements combined?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp; ...
&amp;nbsp;&amp;nbsp; sqllr1 = "select * from cat where table_name like '" +str(LRName1)+"%'"
&amp;nbsp;&amp;nbsp; sqllr2 = "select * from cat where table_name like '" +str(LRName)+"%'"
&amp;nbsp;&amp;nbsp; sql=sqllr1+" UNION "+sqllr2
&amp;nbsp;&amp;nbsp; curs.execute(sql)

&amp;nbsp;&amp;nbsp; tables = curs.fetchall()
&amp;nbsp;&amp;nbsp; ...&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:44:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521543#M40888</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-11T22:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521544#M40889</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That works, and is probably a little better coding than what I had (didn't know about the "UNION"). I have print statements everywhere just to follow along and make sure the variables are what they are suppose to be. I printed tables and this is what I get:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[('RSID122M_IDS_3_2014', 'TABLE'), ('RSID_122M_IDS_9_2012', 'TABLE')]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is correct (not sure what "TABLE" is but the rest of it is fine), but it didn't assign the table name to the variable "LastRun_Table" and that is what I need. I have print statements for LastRun_Table and nothing came up. Any ideas? Am I going about this all wrong?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Mar 2014 20:01:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521544#M40889</guid>
      <dc:creator>MikePowell</dc:creator>
      <dc:date>2014-03-13T20:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521545#M40890</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, I think I figured it out. After doing more research I found that when you use .fetchone(), .fetchmany(), or .fetchall() it returns tuple of tuple which I needed to convert to a list. I don't know if I saying it correctly or if I understand it exactly or not but it made some sense and I was able to get what I was looking for. Here is what I have now:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; import sys, string, os, cx_Oracle, arcpy, datetime

&amp;nbsp;&amp;nbsp; LRName = "RSID_"+num
&amp;nbsp;&amp;nbsp; LRName1 = "RSID"+num
&amp;nbsp;&amp;nbsp; last_year = str(now.year -1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; Two_years = str(now.year -2)

&amp;nbsp;&amp;nbsp; orcl = cx_Oracle.connect('xxxxx/xxxxx@xxx')
&amp;nbsp;&amp;nbsp; curs = orcl.cursor()
&amp;nbsp;&amp;nbsp; sqllr1 = "select * from cat where table_name like '" +str(LRName1)+"%'"
&amp;nbsp;&amp;nbsp; sqllr2 = "select * from cat where table_name like '" +str(LRName)+"%'"
&amp;nbsp;&amp;nbsp; sqllr = sqllr1+" UNION "+sqllr2
&amp;nbsp;&amp;nbsp; curs.execute(sqllr)

&amp;nbsp;&amp;nbsp; tables = [item[0] for item in curs.fetchall()]
&amp;nbsp;&amp;nbsp; for table in tables:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last_year in table:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LastRun_Table = table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif Two_years in table:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LastRun = table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LastRun_Table = ""

&amp;nbsp;&amp;nbsp; curs.close()
&amp;nbsp;&amp;nbsp; orcl.close()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank for the help and insight.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:45:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521545#M40890</guid>
      <dc:creator>MikePowell</dc:creator>
      <dc:date>2021-12-11T22:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521546#M40891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Mike,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"UNION" is a sql command (more info &lt;/SPAN&gt;&lt;A href="http://www.w3schools.com/sql/sql_union.asp"&gt;here&lt;/A&gt;&lt;SPAN&gt;). I am happy you were able to figure it out! Nice work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just one more comment. In the lines "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if last_year in table:&lt;/SPAN&gt;&lt;SPAN&gt;" and "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;elif Two_years in table:&lt;/SPAN&gt;&lt;SPAN&gt;", you are tested to see if the a year is anywhere in the title. It looks like your tables end with the year, but also have numbers throughout the title. I'm just worried that you might get a 'false positive' if you happen to have a table like "RSID_1&lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;2013&lt;/SPAN&gt;&lt;SPAN&gt;M_IDS_9_2008". I'm not sure if this is possible for your data, but it might be safer to use the lines "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if table.endswith(last_year)&lt;/SPAN&gt;&lt;SPAN&gt;" and "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;elif table.endswith(Two_years)&lt;/SPAN&gt;&lt;SPAN&gt;" instead.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Mar 2014 17:16:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521546#M40891</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-03-14T17:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Finding multiple tables and setting them to a variable</title>
      <link>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521547#M40892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good point. I don't that will ever come up with this data but should try to count all issues. Appreciate all the help. Thanks again&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Mar 2014 18:17:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-multiple-tables-and-setting-them-to-a/m-p/521547#M40892</guid>
      <dc:creator>MikePowell</dc:creator>
      <dc:date>2014-03-14T18:17:37Z</dc:date>
    </item>
  </channel>
</rss>

