are there pros or contras, keeping a connection to a (sqlite) database ?

Gerhard Häring gh at ghaering.de
Thu Sep 9 18:16:58 EDT 2010


On Thu, Sep 9, 2010 at 12:29 AM, CM <cmpython at gmail.com> wrote:
> [...]
> I'm not even sure what a "connection" really is; I assumed it was
> nothing more than a rule that says to write to the database with the
> file named in the parentheses. [...]

The following list is not exclusive, but these are the first things
that I can think of.

- SQLite connections have state like uncommitted transactions
- SQLite connections have page caches and metadata caches (tables,
structures and indices)

Opening and closing SQLite connections is very cheap, but keeping
connections open is usually a better approach. You have to have a
"global" setting like the path to the database file anyway; so you can
wrap a "global" connection object in a factory function just as well.

In database applications that use the raw DB-APi i usually start with
something like:

def get_con():
   # return database connection
   ...

HTH

-- Gerhard



More information about the Python-list mailing list