在空表中查找SQLite列名 [英] Find SQLite Column Names in Empty Table
问题描述
对于踢,我写一个模式文档工具,生成一个数据库中的表和关系的描述。我现在shimming它与SQLite工作。
For kicks I'm writing a "schema documentation" tool that generates a description of the tables and relationships in a database. I'm currently shimming it to work with SQLite.
我设法提取SQLite数据库中的所有表的名称通过查询 sqlite_master
表。对于每个表名,我然后触发一个简单的
I've managed to extract the names of all the tables in a SQLite database via a query on the sqlite_master
table. For each table name, I then fire off a simple
select * from <table name>
查询,然后使用 sqlite3_column_count()
和 sqlite3_column_name()
用于收集列名称的API,我进一步馈送到 sqlite3_table_column_metadata()
很简单,对吗?
query, then use the sqlite3_column_count()
and sqlite3_column_name()
APIs to collect the column names, which I further feed to sqlite3_table_column_metadata()
to get additional info. Simple enough, right?
问题是它只适用于非空的表。也就是说, sqlite_column _ *()
API仅在 sqlite_step()
返回
The problem is that it only works for tables that are not empty. That is, the sqlite_column_*()
APIs are only valid if sqlite_step()
has returned SQLITE_ROW
, which is not the case for empty tables.
因此,问题是,如何发现空表的列名?SQLite_ROW
So the question is, how can I discover column names for empty tables? Or, more generally, is there a better way to get this type of schema info in SQLite?
我想有一个隐藏的 sqlite_xxx
表潜伏在某处包含此信息,但到目前为止还没有找到它。
I feel like there must be another hidden sqlite_xxx
table lurking somewhere containing this info, but so far have not been able to find it.
推荐答案
sqlite> .header on
sqlite> .mode column
sqlite> create table ABC(A TEXT, B VARCHAR);
sqlite> pragma table_info(ABC);
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 A TEXT 0 0
1 B VARCHAR 0 0
这篇关于在空表中查找SQLite列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!