-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
60 lines (51 loc) · 1.88 KB
/
database.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sqlite3
class Database:
# Erstellen eines Singletons für die Datenbankklasse
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(Database, cls).__new__(cls)
cls._instance.connection = sqlite3.connect('fitness_tracker.db')
return cls._instance
# Db Connection für alle Methoden und Klassenmethoden die eine Connection brauchen, Cursor einfach im Scope erstellen!
def get_connection(self):
return self.connection
def create_tables(self):
# Erstellen von Tabellen, falls noch nicht existieren
cursor = self.get_connection().cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS workouts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
activity TEXT,
duration INTEGER,
calories INTEGER,
date TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS meals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
meal_name TEXT,
calories INTEGER,
date TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS weight_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
weight REAL,
goal_weight REAL, -- Neue Spalte für das Zielgewicht
date TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INT,
weight INT,
fl text
)
''')
# (Weitere Tabellen hier)
self.connection.commit()