How to Show All Tables in MySQL using Python?

How to Show All Tables in MySQL using Python?

To show all tables in a MySQL database using Python, you can make use of the mysql-connector-python package. Here's how you can do it step-by-step:

  • Install the required package:
pip install mysql-connector-python 
  • Connect to the MySQL database and fetch tables:
import mysql.connector # Database connection configuration config = { 'user': 'your_username', 'password': 'your_password', 'host': '127.0.0.1', 'database': 'your_database_name', 'raise_on_warnings': True } # Establish the connection conn = mysql.connector.connect(**config) cursor = conn.cursor() # Fetch and print all tables in the database cursor.execute("SHOW TABLES") for table in cursor.fetchall(): print(table[0]) # Close the cursor and connection cursor.close() conn.close() 

Replace 'your_username', 'your_password', and 'your_database_name' with the appropriate values for your MySQL setup.

After running the above code, you'll see a list of all table names in the specified MySQL database printed to the console.


More Tags

color-space facebook blobs feign azure-configuration advanced-custom-fields flutter-row weak-references kill werkzeug

More Programming Guides

Other Guides

More Programming Examples