<?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: MySql: Loop through list of tables and return number of rows in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110018#M62761</link>
    <description>&lt;P&gt;Yeah.&amp;nbsp; Perhaps.&amp;nbsp; Emphasis on &lt;EM&gt;&lt;STRONG&gt;HACK&lt;/STRONG&gt;&lt;/EM&gt; in my earlier post.&amp;nbsp; There comes a point where a guy says, "&lt;STRONG&gt;&lt;EM&gt;yep, good enough"....&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Oct 2021 22:28:12 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2021-10-21T22:28:12Z</dc:date>
    <item>
      <title>MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109915#M62748</link>
      <description>&lt;P&gt;Back in 2020 I was messing around with python and mySql. (&lt;A href="https://community.esri.com/t5/python-questions/passing-a-python-variable-to-a-my-sql-querie/m-p/257878" target="_self"&gt;see this post&lt;/A&gt;) and today I'm back to it.&lt;/P&gt;&lt;P&gt;I can get a list of tables in my database like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import mysql.connector as mysql

cn = mysql.connect(user = 'myUserName', password = 'myPassword', host = 'myHost', database = 'master_db')
cursor = cn.cursor()

cursor.execute("Show tables;")
myresult = cursor.fetchall()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;myresult is a list of tuples that looks a little like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;[('accessmoderules',),
 ('accessmodes',),
 ('activitylogs',),
  ...
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do is loop through myresult and return the table name and the number of records right next to it, but can't quite seem to get it right.&amp;nbsp; If I try this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for table in myresult:
    query = f""" SELECT * FROM from master_db.{table[0]};"""
    number_of_rows = cursor.execute(query)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ProgrammingError: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server 
version for the right syntax to use near 'from master_db.accessmoderules' 
at line 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can run that very query in a MySql Workbench window and get what I want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JoeBorgione_0-1634845293557.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/25801i3DA0A8FA1A65BC7A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JoeBorgione_0-1634845293557.png" alt="JoeBorgione_0-1634845293557.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What am I missing on the python side of things?&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/126818"&gt;@RandyBurton&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 19:44:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109915#M62748</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-10-21T19:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109927#M62750</link>
      <description>&lt;PRE&gt;FROM from&lt;/PRE&gt;&lt;P&gt;is this sql?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 20:06:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109927#M62750</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-10-21T20:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109929#M62751</link>
      <description>&lt;P&gt;mySql&lt;/P&gt;&lt;P&gt;Took care of the double From and get a different error...&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;File "C:\Clones\Pro282\lib\site-packages\mysql\connector\connection.py", line 1169, in handle_unread_result&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;raise errors.InternalError("Unread result found")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;InternalError: Unread result found&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 20:10:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109929#M62751</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-10-21T20:10:37Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109973#M62754</link>
      <description>&lt;P&gt;What happens if you take out the semicolon at the end of the sql?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 20:57:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109973#M62754</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-21T20:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109999#M62755</link>
      <description>&lt;P&gt;finally got this hack to give me what I want:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import mysql.connector as mysql

cn = mysql.connect(user = 'user', password = 'pwd', host = 'host', database = 'master_db')
cursor = cn.cursor()

cursor.execute("Show tables")
tables = cursor.fetchall()
for table in tables:
    cursor.execute(f"select * from master_db.{table[0]}")
    rows = cursor.fetchall()
    print(f'{table[0]}, {len(rows)}')&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 21:43:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1109999#M62755</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-10-21T21:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110005#M62757</link>
      <description>&lt;P&gt;so it was From from&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 22:01:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110005#M62757</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-10-21T22:01:07Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110007#M62759</link>
      <description>&lt;P&gt;Among other things....&lt;/P&gt;&lt;P&gt;Soon I won't be able to say:&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 22:03:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110007#M62759</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-10-21T22:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110016#M62760</link>
      <description>&lt;P&gt;Wouldn't it be more efficient to use &lt;A href="https://www.mysqltutorial.org/mysql-count/" target="_self"&gt;count()&lt;/A&gt; instead of reading all the data?&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;select COUNT(*) from owner.table_name&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 22:24:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110016#M62760</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-21T22:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110018#M62761</link>
      <description>&lt;P&gt;Yeah.&amp;nbsp; Perhaps.&amp;nbsp; Emphasis on &lt;EM&gt;&lt;STRONG&gt;HACK&lt;/STRONG&gt;&lt;/EM&gt; in my earlier post.&amp;nbsp; There comes a point where a guy says, "&lt;STRONG&gt;&lt;EM&gt;yep, good enough"....&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 22:28:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110018#M62761</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-10-21T22:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110020#M62762</link>
      <description>&lt;P&gt;You do you, dude &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 22:29:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110020#M62762</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-21T22:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: MySql: Loop through list of tables and return number of rows</title>
      <link>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110096#M62765</link>
      <description>&lt;P&gt;You might try this MySQL query which should return both table names and associated row counts for a database:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'yourDatabaseName';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Oct 2021 04:15:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mysql-loop-through-list-of-tables-and-return/m-p/1110096#M62765</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-10-22T04:15:37Z</dc:date>
    </item>
  </channel>
</rss>

