-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
38 lines (29 loc) · 949 Bytes
/
Copy pathdatabase.py
File metadata and controls
38 lines (29 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sqlite3
DB_NAME = "workouts.db"
def get_connection():
return sqlite3.connect(DB_NAME)
def init_db():
with get_connection() as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS workouts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
exercise TEXT,
duration INTEGER,
notes TEXT
)
""")
conn.commit()
def add_workout(date, exercise, duration, notes):
with get_connection() as conn:
conn.execute(
"INSERT INTO workouts (date, exercise, duration, notes) VALUES (?, ?, ?, ?)",
(date, exercise, duration, notes)
)
conn.commit()
def get_all_workouts():
with get_connection() as conn:
cursor = conn.execute(
"SELECT id, date, exercise, duration, notes FROM workouts ORDER BY id DESC"
)
return cursor.fetchall()