【发布时间】:2013-08-03 14:32:47
【问题描述】:
我想列出一个 sqlite3 数据库的所有表,但它不起作用。假设在 ma 数据库中,我有以下表:foo1, foo2, foo3, ... 然后,使用此代码:
cur.execute("SELECT name FROM sqlite_master")
for table in cur:
print("The table is", table[0])
我有这个输出:The table is 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
那么我的代码有什么问题?
【问题讨论】:
-
我不这么认为。我已经使用您提供的链接来制作我的代码,但它不起作用。
-
也许按名字订购?
SELECT name FTOM sqlite_master ORDER BY name -
对于第三方来说,从问题中编辑语法错误通常是一个坏主意,因为它们通常会提供错误的线索,所以我将其回滚。 @Shan-x 如果
FTOM实际上不在您的代码中,我建议您编辑问题。如果这些是拼写错误,我建议您复制并粘贴您的代码,而不是重新输入。 -
将光标当作迭代器应该可以正常工作,而且
cur.fetchone()似乎可以工作,那么您是否尝试过测试cur.fetchall()?