2.4. floodestimation.db — Storing gauged catchment data in sqlite database

This module provides a connection with an sqlite database to store (gauged) catchment data including annual maximum flow data and catchment descriptors.

The database connection usses the sqlalchemy package (docs) for interaction with a sqlite database. The module contains a Base class that all entities in floodestimation.entities are based on. This enables straightforward retrieving and saving of data in relevant tables.

The sqlite database is saved in the user’s application data folder. On Windows, this is folder is located at C:\Users\{Username}\AppData\Local\Open Hydrology\fehdata\fehdata.sqlite.

The database is automatically created (if it does not exist yet) when importing the floodestimation package.

Interaction with the database is typically as follows:

from floodestimation import db
from floodestimation.entities import Catchment

# Once:
session = db.Session()

# As and when required:
session.add(Catchment(...))                           # Load data
catchments = session.query(Catchment).filter_by(...)  # Retrieve data
session.commit()

Typically a single session instance can be used throughout a program with commits (or rollbacks) as and when required.

class floodestimation.db.Base(**kwargs)

Base class all entities that should be stored as a table in the database should be inheriting from. For example:

from floodestimation import db

class SomeEntity(db.Base):
    __tablename__ = 'some_entity'
    ...
floodestimation.db.empty_db_tables()[source]

Empty all database tables.