Skip to main content
 首页 » 编程设计

python之列出数据库的表

2024年11月01日9落叶无声

<分区>

我想列出一个sqlite3数据库的所有表,但是不行。假设在 ma 数据库中有以下表:foo1、foo2、foo3、…… 然后,使用此代码:

cur.execute("SELECT name FROM sqlite_master") 
for table in cur: 
     print("The table is", table[0]) 

我有这个输出:表是 foo1,而我想要这个:

The table is foo1 
The table is foo2 
The table is foo3 
… 

但是如果我将代码修改为:

cur.execute("SELECT name FROM sqlite_master") 
print(cur.fetchone()) 
for table in cur: 
     print("The table is", table[0] 

那么输出是:

(foo1,) 
The table is foo2 

那么我的代码有什么问题呢?