Without much experience I can theorize with the best of them. Is there any chance that those database close and open operations are asynchronous - if the loop is tight the close might not have finished and the db can't be opened again?
And have I understood correctly that you've isolated problem completely to the sqlite part - no mysql involvement?
This must be immensely frustrating.
I'd suggest writing up a minimal test case that shows the same behaviour if you have the time - you can submit it here & get it checked by people like Ludo and/or upload it to the bug tracker.
Alternatively, you might switch from sqlite to Firebird embedded. Works well on Windows for me (small scale though), might work well on Linux as well.
Good luck!