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)
現在、2つのオプションがあります。
tables = cursor.fetchall() # return data from last query
またはカーソルを反復します。
for (table_name,) in cursor:
print(table_name)
show tables
役立ちます。 これがドキュメントです.
所属していません StackOverflow