Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

SQLite: Count the number of records in multiple tables with a shell script

Background

In SQLite, you can list the name of tables with the .tables command or the following SQL:

SELECT name FROM sqlite_master WHERE type = 'table' and name NOT LIKE 'sqlite_%'

Problem

You can't list the number of columns of multiple tables with SQL because all table names in a SQL statement need to be known at the compile time (see https://stackoverflow.com/a/59160460/3902663).

Solution

You can list the table names and the number of their records with the following shell script:

for i in `sqlite3 AdventureWorks.db ".tables"`; do echo $i; sqlite3 AdventureWorks.db "SELECT COUNT(*) FROM $i"; done