如何获取 SQLite 数据库表中的列列表?

2021-12-08 00:00:00 list reflection sqlite

我希望检索表中的列列表.该数据库是 SQLite 的最新版本(我相信是 3.6).我正在寻找使用 SQL 查询执行此操作的代码.与列相关的元数据(例如长度、数据类型等...)的额外加分

I am looking to retrieve a list of columns in a table. The database is the latest release of SQLite (3.6, I believe). I am looking for code that does this with a SQL query. Extra bonus points for metadata related to the columns (e.g. length, data type, etc...)

推荐答案

您要查找的内容称为数据字典.在 sqlite 中,可以通过查询 sqlite_master 表(或视图?)找到所有表的列表

What you're looking for is called the data dictionary. In sqlite a list of all tables can be found by querying sqlite_master table (or view?)

sqlite> create table people (first_name varchar, last_name varchar, email_address varchar);
sqlite> select * from sqlite_master;
table|people|people|2|CREATE TABLE people (first_name varchar, last_name varchar, email_address varchar)

要获取列信息,您可以使用 pragma table_info(table_name) 语句:

To get column information you can use the pragma table_info(table_name) statement:

sqlite> pragma table_info(people);
0|first_name|varchar|0||0
1|last_name|varchar|0||0
2|email_address|varchar|0||0

有关 pragma 语句的更多信息,请参阅文档.

For more information on the pragma statements, see the documentation.

相关文章