apsw: Another Python SQLite Wrapper
github.com
github.com
My normal mode of operation is via postgres(I like the functions better than sqlite) But for a recent little program thought it would be nice to have it a little more self contained and allow for a sqlite db if wanted. Ignoring the the sql differences(Remember, I use postgres for it's functions, so there were many) sqlite uses a different parameter style(I ended up writing an automatic converter to transform my psycopg3 style "%(name)s" to sqlite3's ":name") and I had got in the (perhaps bad) habit of keeping an open connection and using a context manager on the cursor where sqlite does not like that and really wants the context manager on the connection. The final hoop to jump through was getting the row output as a dict. Not super relevant as it is not default on psycopg either, but it takes a bit more effort on sqlite3.
So yes it is possible to make the database swapable, But even with the dbapi, it is not trivial.
I've been using it a lot over the last year or two and it's worked great.
(This documentation page explains the library's intent: https://rogerbinns.github.io/apsw/pysqlite.html)
https://github.com/coleifer/cysqlite
This supersedes my earlier pysqlite3 project. Perf is better than stdlib, but not as good as apsw. Roger is doing amazing work and I urge anyone wanting to fully utilize SQLite to try apsw. It does everything, exposes all API surfaces, and has a fantastic track record.
Has anyone ever gone through a phase (or is in a phase), of just writing and reading from files instead of cajoling everything into SQL databases and wrangling stringy queries?
I find you can just make files with your own file formats and be fine, as long as your magic numbers make a cool mnemonic when read as hex.
I'm sure I'll face a huge wall at some point in my development career, or I'll be forced to adopt SQL when inheriting a codebase, but I don't quite know what the main issue with this approach is with this. For the time being, whenever I read articles or Readmes like this it feels like my approach at least has the pro of not needing "yet another Python SQlite wrapper"
On the cons, you have sql injections, which can kill your company's reputation.
It only became less common by shifting the issue to the supply chain, now you need orms and yet another wrangling library. So I'm not convinced the security issue is "fixed", let alone the code complexity introduced by these things just to effectively do one of the most fundamental operations in computing, writing() to disk
That forced me to use a database, because now I could use triggers to automatically save off old values, and views to use them nicely.
There were many Python SQLite wrappers at the time, but I didn't like them. So I wrote another one :)
https://www.rogerbinns.com/blog/20-years-of-apsw.html
(I am the APSW maintainer)
For your consideration (I'll add that I used to think similarly as you, and I still wouldn't use sqlite for everything, but it's shockingly effective):
https://www.sqlite.org/transactional.html
https://www.sqlite.org/fasterthanfs.html
https://www.sqlite.org/atomiccommit.html
https://www.sqlite.org/aff_short.html
Immediately the maintainer responded with a very thorough set of benchmarks and improvements!
The nicest part was that it turns out we lived in the same small-ish town and I didn't even know it.
As you found out this happens because the cursor is immediately exhausted as there are no rows, and then is cleaned up and so column data names are not available.
You can use apsw.ext.query_info to get details about a query without running it - see https://rogerbinns.github.io/apsw/ext.html#apsw.ext.query_in... which includes an example.
Behind the scenes you can use an execution tracer to extract the information. That is what query_info does. although it aborts execution after recording it.
(I am the APSW maintainer)
It’s a fantastic tool, with much better defaults, but man that tiny thing irks me every time I use it.
There is even a website that tracks around 150 different Linux and BSD flavours (even termux) and shows package name, version, etc for each it is present on - see https://repology.org/project/python:apsw/versions
(I am the APSW maintainer)
(I am the APSW author)