我刚刚开始学习SQLite。如果能够看到表的详细信息就太好了,比如MySQL的DESCRIBE [table]。PRAGMA table_info [table]不够好,因为它只有基本信息(例如,它没有显示一个列是否是某种类型的字段)。SQLite有办法做到这一点吗?
您正在寻找用于生成表的SQL吗?为此,你可以查询sqlite_schema表:
sqlite> CREATE TABLE foo (bar INT, quux TEXT);
sqlite> SELECT * FROM sqlite_schema;
table|foo|foo|2|CREATE TABLE foo (bar INT, quux TEXT)
sqlite> SELECT sql FROM sqlite_schema WHERE name = 'foo';
CREATE TABLE foo (bar INT, quux TEXT)
选择的名字 模式表总是可以使用名称sqlite_schema来引用,特别是在使用模式名(如main)限定的情况下。Sqlite_schema或temp.sqlite_schema。但为了历史兼容,一些替代名称也被认可,包括: sqlite_master sqlite_temp_schema sqlite_temp_master 替代方案(2)和(3)仅适用于与每个数据库连接关联的TEMP数据库,但替代方案(1)适用于任何地方。
为了防止人们被一些评论误导到其他答案:
如果.schema或sqlite_master的查询没有给出任何输出,则表示表名不存在,例如,这也可能是由;.schema, .tables,…或者只是因为这个表根本不存在。 schema不能正常工作是不太可能的,然后应该在sqlite项目中提交错误报告。
... .模式只能在命令行中使用;上面的命令>可以通过库(Python, c#等)作为查询运行。——Mark Rushakoff 7月25日21:09
'can only be used from a command line' may mislead people. Almost any (likely every?) programming language can call other programs/commands. Therefore the quoted comment is unlucky as calling another program, in this case sqlite, is more likely to be supported than that the language provides a wrapper/library for every program (which not only is prone to incompleteness by the very nature of the masses of programs out there, but also is counter acting single-source principle, complicating maintenance, furthering the chaos of data in the world).
如果你使用图形工具。它在表名旁边显示模式。对于DB Browser For Sqlite,单击打开数据库(右上角),导航并打开您的数据库,您将看到填充在表中的信息如下所示。
右键单击record/table_name,单击copy create statement,你就有了它。
希望它能帮助到一些无法使用命令行的初学者。
".schema可以比PRAGMA显示更多的表细节,包括表约束。
下面的命令显示所有表的详细信息:
.schema
下面的命令以良好的格式显示了所有表的详细信息:
.schema --indent
下面的命令显示了一个表的详细信息:
.schema <table_name>
下面这些命令以一种格式良好的方式显示一个表的详细信息:
.schema --indent <table_name>
Or:
.schema <table_name> --indent
此外,下面这些命令显示了关于".schema"的详细信息:
.help .schema
Or:
.help schema
然后,它是这样的:
sqlite> .help .schema
.schema ?PATTERN? Show the CREATE statements matching PATTERN
Options:
--indent Try to pretty-print the schema
--nosys Omit objects whose names start with "sqlite_"
推荐文章
- 如何从命令行在windows中找到mysql数据目录
- 如何找到MySQL的根密码
- 如何改变字符集(和排序)在整个数据库?
- mySQL:: insert到表,数据从另一个表?
- 重复键忽略?
- 将表从一个数据库复制到另一个数据库的最简单方法?
- 什么是分片,为什么它很重要?
- 如何通过查询在MySQL中获得数据库结构?
- MySQL删除表中的所有行,并将ID重置为零
- 在准备语句中使用“like”通配符
- MySQL中的表名是否区分大小写?
- 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
- 数据库触发器是必要的吗?
- 如何知道MySQL表最近一次更新?
- 如何转储一些SQLite3表的数据?