Using sqlite-diffable as a Python module
Posted: | Tags: til sqlitesqlite-diffable
is a tool, built by Simon Willison, to load and dump SQLite databases to JSON files. It’s intended to be used through the CLI, however since May I’ve been using this tool as a callable Python module to build my main site after making changes to the code1. This has allowed me to combine the build commands into one Python file. The changes aren’t available in the upstream code2 so it’ll have to be pulled from my fork if you’re looking to use this.
Install
Install using pip directly
pip install sqlite-diffable @ git+https://github.com/PeskyPotato/sqlite-diffable.git@py_module
Install from a requirements.txt file
# requirements.txt
sqlite-diffable @ git+https://github.com/PeskyPotato/sqlite-diffable.git@py_module
pip install -r requirements.txt
Usage
Dumping all tables from a database
Dump all tables from a database file called posts.db
into the ./content/posts/
directory.
sqlite_diffable.dump('posts.db', './content/posts/', all=True)
Dump specific database tables
Dump tables “posts” and “tags” from a database file called posts.db
into the ./content/posts/
directory.
sqlite_diffable.dump('posts.db', 'dump/', tables=["posts", "tags"])
Loading a database
Load a database from JSON files located at ./content/posts/
and name the database file links.db
.
sqlite_diffable.load('posts.db', './content/posts/')
If the tables already exist in the database, you can overwrite them by setting replace=True
.
sqlite_diffable.load('links.db', './content/posts/', replace=True)
Simon uses Click to create the command line interface for sqlite-diffable which doesn’t make it very easy to create a Python module off from. I was able to use an example from Massimo Frasson on StackOverflow to be able to reuse the code written for the Click CLI. ↩︎
I did create a PR today and an issue describing the changes in detail back in May. ↩︎