gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:20:03 24 4
gpt4 key购买 nike

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

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

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

OperationalError: near "SHOW": syntax error

我查看了 SHOW TABLES 文档以及网络,但没有找到对我有帮助的信息。

最佳答案

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

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 FAQ了解更多信息。

关于python - 在 Python 中显示 SQLite 数据库中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com