如何使用Python和MySQLDB在MySQL数据库中检索表名称?
-
30-09-2019 - |
题
我有一个SQL数据库,想知道您使用哪种命令来获取该数据库中的表名列表。
解决方案
显示表
15个字符
其他提示
更完整:
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)
现在有两个选择:
tables = cursor.fetchall() # return data from last query
或在光标上迭代:
for (table_name,) in cursor:
print(table_name)
show tables
会有所帮助。 这是文档.
不隶属于 StackOverflow