diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-11 21:16:57 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-11 21:47:56 +0100 |
| commit | 6e7e00846e658cb79d0c23e18939c59fedba06dd (patch) | |
| tree | 72b9565842186c66a2837ff3561b1010020daebc /db.py | |
| download | EgoMetrics-6e7e00846e658cb79d0c23e18939c59fedba06dd.tar.gz EgoMetrics-6e7e00846e658cb79d0c23e18939c59fedba06dd.zip | |
Add workout logging CLI with SQLite storage
Exercises CRUD, session logging with sets/reps/RPE/rest/LSRPE,
session viewing and deletion. Interactive terminal menu.
Diffstat (limited to 'db.py')
| -rw-r--r-- | db.py | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -0,0 +1,52 @@ +import sqlite3 +import os + +DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "egometrics.db") + +SCHEMA = """ +CREATE TABLE IF NOT EXISTS exercises ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE COLLATE NOCASE, + note TEXT +); + +CREATE TABLE IF NOT EXISTS sessions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + date_time TEXT NOT NULL, + note TEXT +); + +CREATE TABLE IF NOT EXISTS session_exercises ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + session_id INTEGER NOT NULL, + exercise_id INTEGER NOT NULL, + position INTEGER NOT NULL, + sets INTEGER, + reps INTEGER, + rpe INTEGER, + rest_time INTEGER, + lsrpe INTEGER, + note TEXT, + FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE, + FOREIGN KEY (exercise_id) REFERENCES exercises(id) ON DELETE RESTRICT +); + +CREATE INDEX IF NOT EXISTS idx_session_exercises_session + ON session_exercises(session_id); + +CREATE INDEX IF NOT EXISTS idx_sessions_date + ON sessions(date_time); +""" + + +def get_connection() -> sqlite3.Connection: + conn = sqlite3.connect(DB_PATH) + conn.row_factory = sqlite3.Row + conn.execute("PRAGMA foreign_keys = ON") + return conn + + +def init_db() -> None: + conn = get_connection() + conn.executescript(SCHEMA) + conn.close() |
