在Python中显示SQLite数据库中的表 [英] Show Tables in SQLite Database in Python

查看:94
本文介绍了在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.

这篇关于在Python中显示SQLite数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆