Select 请求中的杂注

Pragma in Select request

我想在 SQLite 的 table 中检索列名和行数。 是否可以通过一条命令做到这一点?

SELECT COUNT(*) FROM tablename

PRAGMA table_info(tablename))

PRAGMA 是一个单独的命令,不是(子)查询,不能与其他 SQL 语句组合。

接受的答案是正确的,但现在已经过时了,因为 SQLite 3.16.0 你可以使用 PRAGMA functions

所以你现在可以写:

SELECT * FROM pragma_table_info('tablename') JOIN (SELECT COUNT(*) FROM tablename);

或者如果你真的只需要名字:

SELECT name, rowcount FROM pragma_table_info('tablename') JOIN (SELECT COUNT(*) AS rowcount FROM tablename);

(在 SQLite 3.25.2 中测试)

但使用它需要您自担风险:

This feature is experimental and is subject to change. Further documentation will become available if and when the table-valued functions for PRAGMAs feature becomes officially supported.