Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.4 kB
1
Indexable
Never
"""Handles the database for the MEASUREMENTS web site"""
import sqlite3

__MEASUREMENTS_DB = "measurements.db"
__CREATE_SQL = """
CREATE TABLE IF NOT EXISTS measurements
(latest_TVOC FLOAT,
 latest_eCO2 FLOAT,
 max_TVOC FLOATL,
 min_TVOC FLOAT,
 max_eCO2 FLOAT,
 min_eCO2 FLOAT);
"""


def create_database():
    """Creates the database"""
    with sqlite3.connect(
        __MEASUREMENTS_DB, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
    ) as conn:
        cur = conn.cursor()
        cur.execute(__CREATE_SQL)
        conn.commit()


def store_measurement(latest_TVOC: float, latest_eCO2: float, max_TVOC: float, min_TVOC: float, max_eCO2: float, min_eCO2: float) -> int:
    """Stores a measurement in the DB

    Args:
        temperature (float): temp measurement
        humidity (float): humidity measurement
        pressure (float): pressure measurement

    Returns:
        int: the number of rows affected (should be 1)
    """
    with sqlite3.connect(
        __MEASUREMENTS_DB, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
    ) as conn:
        cur = conn.cursor()
        cur.execute(
            "INSERT INTO MEASUREMENTS VALUES (?,?,?,?,?,?)",
            (latest_TVOC, latest_eCO2, max_TVOC, min_TVOC, max_eCO2, min_eCO2),
        )
        conn.commit()
        return cur.rowcount


if __name__ != "__main__":
    create_database()