I have an SQL database and am wondering what command you use to just get a list of the table names within that database.
The syntax to get all table names with the help of SELECT statement. mysql> use test; Database changed mysql> SELECT Table_name as TablesName from information_schema.
In MySQL, there are two ways to find the names of all tables, either by using the "show" keyword or by query INFORMATION_SCHEMA. In the case of SQL Server or MSSQL, You can either use sys. tables or INFORMATION_SCHEMA to get all table names for a database.
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
15 chars
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With