Pergunta

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

Foi útil?

Solução

SHOW tables

15 chars

Outras dicas

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)

show tables will help. Here is the documentation.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top