SQLite Forum

Error: database disk image is malformed for every large database after a few SELECT instruction
Have you checked https://sqlite.org/howtocorrupt.html for things you may be doing?

Have you tried to execute the SELECTs in the SQLite shell?

Most probably some other process is overwriting your file. Try lsof or equivalent to see who else may be acessing your file.