Should I be keeping sqlite connections open or should I close it every time I'm done with my batch queries?
Update: This is specific to iPhone/iOS if that makes a difference.
The sqlite3 module connect() ; the connection must be closed at the end of the session with the . close() command. While the connection is open, any interactions with the database require you to make a cursor object with the . cursor() command.
If you write/read every second I would definitely keep the connection open. If you only access the DB every hour I would probably close the connection. In most cases I would tend to keep the connection open as long as your application runs.
The SQLite connection object remains open until you close it using the close function. Always close this object when you finish using it.
SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage.
My general recommendation given that the DB interaction is user driven, I'd say open, do your activities, close it and then return to the user. This leaves nothing up in the air. There tends to be an over-emphasis on performance. The best approach on single user applications I find is to assume that performance is going to be fine, and then prove it otherwise.
One of the ways in which to prove this to yourself is right some test code to time how long it's taking to open and close a connection. Next is to try to sets of actions, one with each strategy. This should convince you.
The downsides to leaving your connection open generally outweighs (again, in single user applications) the benefits of keeping it open.
That's my 2 cents,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With