How do I drop all empty tables from a MySQL database, leaving only the tables that have at least 1 record?
EDIT: This is what I did using Python 3 with mysqlclient
import MySQLdb
conn = MySQLdb.connect(
host='localhost', user='root',
passwd='mypassword', db='mydatabase'
)
c = conn.cursor()
c.execute('SHOW TABLES')
for row in c.fetchall():
table_name = row[0]
c.execute(f'SELECT * FROM {table_name}')
if c.rowcount == 0:
# c.execute(f'DROP TABLE {table_name}')
print(f'DROP TABLE {table_name}')
And a PHP version for completeness. It won’t drop anything, just print for you the DROP statements: