如何使用Python和MySQLdb检索mysql数据库中的表名?

Ric*_*ard 25 python mysql mysql-python

我有一个SQL数据库,我想知道你用什么命令来获取该数据库中的表名列表.

Rem*_*emi 62

更完整一点:

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)
Run Code Online (Sandbox Code Playgroud)

现在有两种选择:

tables = cursor.fetchall()       # return data from last query
Run Code Online (Sandbox Code Playgroud)

或迭代光标:

 for (table_name,) in cursor:
        print(table_name)
Run Code Online (Sandbox Code Playgroud)

  • 绝妙的回答 (2认同)

Ty *_*y W 9

SHOW表

15个字符


Hen*_*ing 7

show tables会有所帮助.这是文档.