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

问题描述:

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

I know this is a very basic question but for some reason I can't get past this one error. I'm trying to show/put all the names of the tables in a database (named 'GData.db') into a variable available_tables in Python. Currently I have the following:

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

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

This gives me the following error for the second-last line:

OperationalError: near "SHOW": syntax error

我已经在网上以及网上浏览过SHOW TABLES文档,但没有找到对我有帮助的信息.

I've looked at the SHOW TABLES documentation as well as around the web but not found information that helps me.

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

The query to list tables in a Sqlite database:

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;

因此您的代码段变为:

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())

有关更多信息,请参见 Sqlite常见问题解答.

See the Sqlite FAQ for more info.