在Python中显示SQLite数据库中的表

Lin*_*nnK 0 python mysql sqlite

我知道这是一个非常基本的问题,但是由于某种原因,我无法摆脱这一错误。我正在尝试显示/将数据库中所有表的名称(名为“ GData.db”)放入available_tablesPython中的变量中。目前,我有以下内容:

con = sql.connect(r'/Users/linnk/Desktop/Results/GData.db')
cur = con.cursor() 
cur.execute("SHOW TABLES IN GData")
available_table=(cursor.fetchall())
Run Code Online (Sandbox Code Playgroud)

这给我倒数第二行的以下错误:

OperationalError: near "SHOW": syntax error
Run Code Online (Sandbox Code Playgroud)

我已经在网上查看了SHOW TABLES文档,但没有找到对我有帮助的信息。

chu*_*ash 6

列出Sqlite数据库中的表的查询:

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
Run Code Online (Sandbox Code Playgroud)

因此,您的代码段变为:

con = sql.connect(r'/Users/linnk/Desktop/Results/GData.db')
mycur = con.cursor() 
mycur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;")
available_table=(mycur.fetchall())
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见Sqlite FAQ