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: sqlite3 -readonly opens a database without any chance of changing it

Database
~/tips/tip-database-sqlite3-readonly

sqlite3 -readonly db.sqlite

Opens an SQLite database file in read-only mode, so a stray UPDATE or DELETE just returns an error.

Looking around in the database of an app is a risky hobby when your finger is one wrong statement away from changing data. SQLite has a flag for it.

sqlite3 -readonly db.sqlite
sqlite3 -readonly db.sqlite "SELECT count(*) FROM users;"

The SQLite shell documentation lists -readonly as an option that opens the database read-only, and says writes will be prohibited. Reading with SELECT works as usual, and a statement like this one ends in an error instead of changing the file:

sqlite> DELETE FROM users;

Inside the shell you can do the same for another file with .open --readonly other.db, as the same documentation describes in the section about opening database files.

It is a good habit before you inspect the database of an app that is running, such as a self-hosted service, because the shell cannot write a thing by accident.

The caveat is that the flag protects against changes made from this shell session only. The app that owns the file keeps writing, so you may see a moving target. A database that is busy can also return a locked error to a reader. If you want a snapshot that is truly yours, copy the file with the app stopped, or use the backup command, and open the copy.

Further reading: SQLite: command-line shell

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link