Reuse SQLite connection or reconnect every time? Reuse SQLite connection or reconnect every time? sqlite sqlite

Reuse SQLite connection or reconnect every time?


The best practice for SQLite is to keep a single connection for the lifetime of your program. Closing and opening the connection will not save much memory because SQLite does not keep much state (the only significant memory usage is the page cache, whose size you can configure), and the time for those operations (such as re-reading the database structure) is much worse than the memory that is going to be used.

As for concurrency, there would be no problem with having multiple open connections (from a single process or from multiple processes), because the database gets locked only while a transaction is active.


The best practice for SQLite is to close your connection after every function and open it once for a function. By closing and opening the connection you will save on memory and the time for those operations is less significant than the memory that is going to be used.

You can refer to this article for the best practice on SQLite.


Or use Content provider, and never worry about opening and closing databases, it will take care of it by its own. - Reference

(Despite popular belief that content providers should only be used if you want to share data across apps, it is quite beneficial if you use it to access local data only.)

Check this discussion here