39

I have an SQL database and am wondering what command you use to just get a list of the table names within that database.

1
  • 6
    just wondering why I got the down vote? I know the question was a newbie question but I thought that those type of questions were allowed on stack over flow?
    – Richard
    Aug 24, 2010 at 13:27

5 Answers 5

86

To be a bit more complete:

import MySQLdb

connection = MySQLdb.connect(
                host = 'localhost',
                user = 'myself',
                passwd = 'mysecret')  # create the connection

cursor = connection.cursor()     # get the cursor


cursor.execute("USE mydatabase") # select the database

cursor.execute("SHOW TABLES")    # execute 'SHOW TABLES' (but data is not returned)

now there are two options:

tables = cursor.fetchall()       # return data from last query

or iterate over the cursor:

 for (table_name,) in cursor:
        print(table_name)
4
  • Pitty... they removed the whole website. So I removed the link, thanks for noting.
    – Remi
    Dec 18, 2014 at 12:38
  • Error syntax error at or near "USE" LINE 1: USE portal
    – code-8
    Feb 13, 2017 at 19:47
  • 2
    fantastic answer Oct 30, 2018 at 21:12
  • Thank you!! When we query and want to result do we always need to use fetchall() ?
    – haneulkim
    Sep 17, 2019 at 6:55
10

SHOW tables

15 chars

0
7

show tables will help. Here is the documentation.

4

It is also possible to obtain tables from a specific scheme with execute the single query with the driver below.

python3 -m pip install PyMySQL
import pymysql

# Connect to the database
conn = pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='my_database')

# Create a Cursor object
cur = conn.cursor()

# Execute the query: To get the name of the tables from a specific database
# replace only the my_database with the name of your database
cur.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = 'my_database'")

# Read and print tables
for table in [tables[0] for tables in cur.fetchall()]:
    print(table)

output:

my_table_name_1
my_table_name_2
my_table_name_3
...
my_table_name_x
1

Here is another answer using the sqlalchemy and pandas libraries

from sqlalchemy import create_engine
import pandas as pd

# Make connection to server
def connect(self):  
    # create sqlalchemy engine
    engine = create_engine("mysql+pymysql://{user}:{pw}@{server}/{db}"
            .format(user=self.username,
            pw=self.password,
            server=self.server,
            db=self.database))
    return engine   

if _name_ == '_main_':
    server = 'myserver'
    database = 'mydatabase'
    username = 'myusername'
    password = 'mypassword'
    table = 'mytable'

    con = connect()
    my_query = f"SHOW COLUMNS FROM {table};"
    df = pd.read_sql_query(sql=my_query, con=con)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.