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)
| 归档时间: |
|
| 查看次数: |
39742 次 |
| 最近记录: |