brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: PRAGMA table_list shows every table and view with its column count

CLI
~/tips/tip-sqlite-pragma-table-list

PRAGMA table_list;

Lists all tables and views of a SQLite database with type, column count and strict flag.

I used to reach for .tables and then run a separate pragma per table to learn anything about it. There is a single pragma that gives the overview in one go, and I wish I had found it sooner.

PRAGMA table_list;

It returns one row per table or view with these columns: schema, name, type, ncol, wr and strict. The type is table, view, shadow or virtual. ncol is the number of columns, wr is 1 for a WITHOUT ROWID table and strict is 1 for a STRICT table.

Because it is a pragma, you can also use it as a table-valued function and filter it with normal SQL:

SELECT name, ncol FROM pragma_table_list
WHERE schema = 'main' AND type = 'table'
ORDER BY name;

That gives you only your own tables, sorted, without the shadow tables that full-text search creates behind your back.

Two caveats. The pragma first appeared in SQLite 3.37.0, so an older sqlite3 binary will not know it. And it lists all attached schemas by default, including internal ones such as sqlite_schema, so filter on schema and name when the noise bothers you. The SQLite docs also say future releases will probably add more columns, so select columns by name instead of relying on their position.

Further reading: SQLite PRAGMA table_list

More CLI tips printf 'GET /\r\n' | hexdump -C hexdump -C -s 512 -n 64 disk.img wget --limit-rate=2m https://example.com/big-file.iso wget --spider https://example.com/file.zip rg 'apiKey' -g '!*.min.js' -g '!package-lock.json' All CLI tips (46) All topics

Link