是否有相当于 MySQL 的 DESCRIBE [table] 的 SQLite?

发布于 2024-09-11 04:36:08 字数 239 浏览 8 评论 0原文

我刚刚开始学习 SQLite。如果能够看到表的详细信息就好了,比如 MySQL 的 DESCRIBE [table]PRAGMA table_info [table] 还不够好,因为它只有基本信息(例如,它不显示列是否是某种类型的字段)。 SQLite 有办法做到这一点吗?

I'm just getting started learning SQLite. It would be nice to be able to see the details for a table, like MySQL's DESCRIBE [table]. PRAGMA table_info [table] isn't good enough, as it only has basic information (for example, it doesn't show if a column is a field of some sort or not). Does SQLite have a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

疯了 2024-09-18 04:36:08

SQLite 命令行实用程序 有一个 .schema TABLENAME 命令向您显示创建语句。

The SQLite command line utility has a .schema TABLENAME command that shows you the create statements.

汹涌人海 2024-09-18 04:36:08
PRAGMA table_info([tablename]);
PRAGMA table_info([tablename]);
ι不睡觉的鱼゛ 2024-09-18 04:36:08

您是否正在寻找用于生成表的 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_schematemp.sqlite_schema 之类的架构名称进行限定时>。但为了历史兼容性,一些替代名称也被认可,包括:

  1. sqlite_master
  2. sqlite_temp_schema
  3. sqlite_temp_master

替代方案 (2) 和 (3) 仅适用于与每个数据库连接关联的 TEMP 数据库,但替代方案 (1) 适用于任何地方。

Are you looking for the SQL used to generate a table? For that, you can query the sqlite_schema table:

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)
Alternative Names

The schema table can always be referenced using the name sqlite_schema, especially if qualifed by the schema name like main.sqlite_schema or temp.sqlite_schema. But for historical compatibility, some alternative names are also recognized, including:

  1. sqlite_master
  2. sqlite_temp_schema
  3. sqlite_temp_master

Alternatives (2) and (3) only work for the TEMP database associated with each database connection, but alternative (1) works anywhere.

沧笙踏歌 2024-09-18 04:36:08

查看所有表:

.tables

查看特定表:

.schema [tablename]

To see all tables:

.tables

To see a particular table:

.schema [tablename]
奈何桥上唱咆哮 2024-09-18 04:36:08

为了防止人们被其他答案的某些评论误导:

  1. 如果 .schemaquery from sqlite_master 没有给出任何输出,则表明不存在 tablename,例如,这也可能是由 .schema.tables、.. 末尾的 ; 分号引起的。或者只是因为该表确实不存在。
    .schema 不起作用的可能性很小,然后应该在 sqlite 项目中提交错误报告。

....schema 只能从命令行使用;上述命令>可以通过库(Python、C# 等)作为查询运行。 – 马克·鲁沙科夫 2010 年 7 月 25 日 21:09

  1. “只能从命令行使用”可能会误导人们。几乎任何(可能是每种?)编程语言都可以调用其他程序/命令。因此,引用的注释是不幸的,因为调用另一个程序(在本例中为 sqlite)比该语言提供包装器/更有可能受到支持code> 对于每个程序(这不仅由于大量程序的本质而容易出现不完整性,而且还违背了单一源原则,使维护变得复杂,进一步加剧了世界数据的混乱)。

To prevent that people are mislead by some of the comments to the other answers:

  1. If .schema or query from sqlite_master not gives any output, it indicates a non-existent tablename, e.g. this may also be caused by a ; semicolon at the end for .schema, .tables, ... Or just because the table really not exists.
    That .schema just doesn't work is very unlikely and then a bug report should be filed at the sqlite project.

... .schema can only be used from a command line; the above commands > can be run as a query through a library (Python, C#, etc.). – Mark Rushakoff Jul 25 '10 at 21:09

  1. '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).
无力看清 2024-09-18 04:36:08

“PRAGMA”相比,“.schema”可以显示更多的表详细信息,包括表约束

下面的命令显示所有表的详细信息

.schema

下面的命令以格式良好的方式显示所有表的详细信息

.schema --indent

下面的命令显示以下的详细信息一张表

.schema <table_name>

下面的这些命令以格式良好的方式显示一张表的详细信息

.schema --indent <table_name>

或者:

.schema <table_name> --indent

此外,下面的这些命令显示有关“.schema”的详细信息< /strong>:

.help .schema

或者:

.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_"

".schema" can show more details of tables including Table Constraints than "PRAGMA".

This command below shows the details of all tables:

.schema

This command below shows the details of all tables in a well-formatted way:

.schema --indent

This command below shows the details of one table:

.schema <table_name>

These commands below show the details of one table in a well-formatted way:

.schema --indent <table_name>

Or:

.schema <table_name> --indent

In addition, these commands below show the details about ".schema":

.help .schema

Or:

.help schema

Then, this is how it looks like below:

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_"
星光不落少年眉 2024-09-18 04:36:08

如果您使用的是图形工具。它会在表名称旁边显示架构。如果是DB Browser For Sqlite,请单击打开数据库(右上角),导航并打开数据库,您将看到表中填充的信息,如下所示以下。

输入图片此处描述

右键单击记录/表名,然后单击复制创建语句即可。

希望它能帮助一些无法使用命令行的初学者。

If you're using a graphical tool. It shows you the schema right next to the table name. In case of DB Browser For Sqlite, click to open the database(top right corner), navigate and open your database, you'll see the information populated in the table as below.

enter image description here

right click on the record/table_name, click on copy create statement and there you have it.

Hope it helped some beginner who failed to work with the commandline.

风流物 2024-09-18 04:36:08

.schema user_meta 对我不起作用。
.schema 确实向我展示了所有表格。

我可以使用以下命令查看 user_meta 表:

sqlite> SELECT * FROM sqlite_master WHERE type='table' AND name='user_meta';
table|user_meta|user_meta|7|CREATE TABLE user_meta (
    id INTEGER NOT NULL, 
    user_id INTEGER NOT NULL, 
    first_name VARCHAR(10), 
    last_name VARCHAR(10), 
    PRIMARY KEY (id), 
    CONSTRAINT fk_user_id FOREIGN KEY(user_id) REFERENCES user (id), 
    UNIQUE (user_id)
)

.schema user_meta didn't work for me.
but .schema did show me all the tables.

I was able to see user_meta table using this command:

sqlite> SELECT * FROM sqlite_master WHERE type='table' AND name='user_meta';
table|user_meta|user_meta|7|CREATE TABLE user_meta (
    id INTEGER NOT NULL, 
    user_id INTEGER NOT NULL, 
    first_name VARCHAR(10), 
    last_name VARCHAR(10), 
    PRIMARY KEY (id), 
    CONSTRAINT fk_user_id FOREIGN KEY(user_id) REFERENCES user (id), 
    UNIQUE (user_id)
)
美胚控场 2024-09-18 04:36:08

例如,我的 Django SQLite 数据库中有这些表:

sqlite3 db.sqlite3

然后

.tables

为了描述和查看 shop_product 表的 SQL 语句,您可以运行以下命令:

.schema shop_product

插图

插图

For example, I have these tables in my Django SQLite database:

sqlite3 db.sqlite3

Then

.tables

In order to describe and see the SQL statements for shop_product table, you can run the following command:

.schema shop_product

Illustration

Illustration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文