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

2024-03-15

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


更完整一点:

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐