我想把我的iPhone应用程序迁移到一个新的数据库版本。由于我没有保存一些版本,我需要检查是否存在某些列名。

这个Stackoverflow条目建议执行选择

SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'

并解析结果。

这是常见的方式吗?选择呢?


当前回答

如果有的话

.headers ON

你会得到你想要的结果。

其他回答

. schema 在sqlite控制台中,你在表中 在我看来是这样的……

sqlite>.schema
CREATE TABLE players(
id integer primary key,
Name varchar(255),
Number INT,
Team varchar(255)

如果你有sqlite数据库,使用sqlite3命令行程序和这些命令:

列出数据库中所有的表。

.tables

要显示给定表名的模式:

.schema tablename

要获得列的列表,您可以简单地使用:

.schema tablename

当你运行sqlite3命令行时,输入:

sqlite3 -header

也会给你想要的结果吗

如果您正在使用SQLite3,则不支持INFORMATION_SCHEMA。使用PRAGMA table_info代替。这将返回关于表的6行信息。要获取列名(row2),使用如下所示的for循环

cur.execute("PRAGMA table_info(table_name)")  # fetches the 6 rows of data
records = cur.fetchall() 
print(records)
for row in records:
    print("Columns: ", row[1])