【问题标题】:Show Tables in SQLite Database in Python用 Python 在 SQLite 数据库中显示表
【发布时间】:2015-11-06 07:41:00
【问题描述】:

我知道这是一个非常基本的问题,但由于某种原因,我无法克服这个错误。我正在尝试将数据库中的所有表名(名为“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 文档以及网络,但没有找到对我有帮助的信息。

【问题讨论】:

标签: python mysql sqlite


【解决方案1】:

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

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

所以你的 sn-p 变成:

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

【讨论】:

  • 谢谢!我跳过了 ORDER BY 部分,但其他方面都很完美。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2022-12-12
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多