Second approach is better when you rarely access the database, also it will make sure releases happen (or at least provide immediate errors if not), but keeping a connection open is much better when hundreds of accesses happen in terms of speed - especially loads of small queries, large queries won't matter either way.

Note: i say "keeping the connection open" but this may well mean as long as the queries are needed and not necessarily for the entire duration of the Program's life.

A last consideration, when it is definitely good to keep open from the start, is when you intend the DB to be locked by your app - granted this is not the normal case, but it might be a requirement from time to time. So keeping the connection open in locked mode for the duration of the program will maintain the lock as well.
"PRAGMA locking_mode = 'Exclusive';   --  Locked"

As far as I am aware, there are no other "timely background maintenance" that sqlite does which would make it somehow more advantageous to resort to a "keep-alive" connection paradigm. Six of one, half-a-dozen of the other.

Salut!
Ryan

On 2014/04/29 00:00, Peter Haworth wrote:
There's a discussion on another forum I'm on about whether it's good
practice to open an SQLite database as part of program initialization and
close it when the program terminates, or whether the connection should be
opened and closed around each transaction.

I've always used the first approach and can't think of any benefits to the
second unless you're simply reading data into memory and never accessing
the database directly again. Are there any other circumstances where the
second approach is better?

Pete
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to