It's probably out of scope for you, but I've used the 'vtfunc' module [0] for a similar purpose actually.

[0] https://github.com/coleifer/sqlite-vtfunc