Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pragma in Select request

Tags:

sql

sqlite

pragma

I wanna to retrieve column's names and rows count in a table in SQLite. Is it possible to do this is one command?

SELECT COUNT(*) FROM tablename

and

PRAGMA table_info(tablename))
like image 304
Vyacheslav Avatar asked Nov 06 '15 13:11

Vyacheslav


2 Answers

The accepted answer was correct, but is now outdated, since SQLite 3.16.0 you can use PRAGMA functions

So you can now write:

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

Or if you really only need the name:

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

(tested in SQLite 3.25.2)

Use it at your own risk though:

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.

like image 198
szmate1618 Avatar answered Sep 20 '22 15:09

szmate1618


A PRAGMA is a separate command, not a (sub)query, and cannot be combined with other SQL statements.

like image 41
CL. Avatar answered Sep 18 '22 15:09

CL.