I've been experimenting with this approach against SQLite for a few years now, and I really like it.
My sqlite-utils package does exactly this. Try running this on the command line:
brew install sqlite-utils
echo '[
{"id": 1, "name": "Cleo"},
{"id": 2, "name": "Azy", "age": 1.5}
]' | sqlite-utils insert /tmp/demo.db creatures - --pk id
sqlite-utils schema /tmp/demo.db
It outputs the generated schema: CREATE TABLE [creatures] (
[id] INTEGER PRIMARY KEY,
[name] TEXT,
[age] FLOAT
);
When you insert more data you can use the --alter flag to have it automatically create any missing columns.Full documentation here: https://sqlite-utils.datasette.io/en/stable/cli.html#inserti...
It's also available as a Python library: https://sqlite-utils.datasette.io/en/stable/python-api.html