
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to show all the tables present in the database and server in MySQL using Python?
We may sometimes require to get the list of all the tables present in our database. This can be done by using the SHOW TABLES command.
The SHOW TABLES command is used to display the table names in a database as well as the server.
Syntax
To show the tables present in a database −
SHOW TABLES
The above statement when executed using the cursor object returns the names of the tables present in our database.
To show the tables present in a server
SELECT table_name FROM information_schema.tables
Steps to show all tables present in a database and server using MySQL in python
import MySQL connector
establish connection with the connector using connect()
create the cursor object using cursor() method
create a query using the appropriate mysql statements
execute the SQL query using execute() method
close the connection
Show the tables present in a database
Example
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your_password",database="database_name") cursor=db.cursor() cursor.execute("SHOW TABLES") for table_name in cursor: print(table_name)
Show the tables present in a server
Example
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your_password",database="database_name") cursor=db.cursor() cursor.execute("SELECT table_name FROM information_schema.tables") for table_name in cursor: print(table_name)
The above codes output the list of tables present in your database or the server .
Output
Employees Students MyTable
- Related Articles
- How can I display all databases in MySQL and for each database show all tables?
- List down all the Tables in a MySQL Database
- Which tables are used to control the privileges of MySQL database server?
- Display all tables inside a MySQL database using Java?
- How to GRANT SELECT ON all tables in all databases on a server with MySQL?
- Get record count for all tables in MySQL database?
- How can we get the list of tables in a particular database from MySQL Server command line?
- How can I describe all tables in the database through a single statement in MySQL?
- How can we analyze the tables of a particular database from MySQL Server command line?
- How to get the list of tables in default MySQL database?
- How to count the number of tables in a MySQL database?
- How can we check the character set of all the tables in a particular MySQL database?
- How to display all the MySQL tables in one line?
- How to copy tables or databases from one MySQL server to another MySQL server?
- How to List All Tables in a Schema in Oracle Database?
