Spaces:
Sleeping
Sleeping
Update database.py
Browse files- database.py +78 -2
database.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
import sqlite3
|
|
|
|
| 2 |
|
|
|
|
|
|
|
| 3 |
def get_connection():
|
| 4 |
-
conn = sqlite3.connect("
|
| 5 |
return conn
|
| 6 |
|
| 7 |
|
|
|
|
| 8 |
def create_tables():
|
| 9 |
conn = get_connection()
|
| 10 |
cur = conn.cursor()
|
|
@@ -28,4 +32,76 @@ def create_tables():
|
|
| 28 |
""")
|
| 29 |
|
| 30 |
conn.commit()
|
| 31 |
-
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
+
from datetime import date
|
| 3 |
|
| 4 |
+
|
| 5 |
+
# ---------------- DATABASE CONNECTION ----------------
|
| 6 |
def get_connection():
|
| 7 |
+
conn = sqlite3.connect("fitplan.db", check_same_thread=False)
|
| 8 |
return conn
|
| 9 |
|
| 10 |
|
| 11 |
+
# ---------------- CREATE TABLES ----------------
|
| 12 |
def create_tables():
|
| 13 |
conn = get_connection()
|
| 14 |
cur = conn.cursor()
|
|
|
|
| 32 |
""")
|
| 33 |
|
| 34 |
conn.commit()
|
| 35 |
+
conn.close()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ---------------- REGISTER USER ----------------
|
| 39 |
+
def register_user(email, password):
|
| 40 |
+
|
| 41 |
+
conn = get_connection()
|
| 42 |
+
cur = conn.cursor()
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
cur.execute(
|
| 46 |
+
"INSERT INTO users(email,password) VALUES (?,?)",
|
| 47 |
+
(email, password)
|
| 48 |
+
)
|
| 49 |
+
conn.commit()
|
| 50 |
+
return True
|
| 51 |
+
except:
|
| 52 |
+
return False
|
| 53 |
+
finally:
|
| 54 |
+
conn.close()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ---------------- LOGIN USER ----------------
|
| 58 |
+
def login_user(email, password):
|
| 59 |
+
|
| 60 |
+
conn = get_connection()
|
| 61 |
+
cur = conn.cursor()
|
| 62 |
+
|
| 63 |
+
cur.execute(
|
| 64 |
+
"SELECT * FROM users WHERE email=? AND password=?",
|
| 65 |
+
(email, password)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
user = cur.fetchone()
|
| 69 |
+
|
| 70 |
+
conn.close()
|
| 71 |
+
|
| 72 |
+
return user
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# ---------------- SAVE WEIGHT ----------------
|
| 76 |
+
def save_weight(email, weight):
|
| 77 |
+
|
| 78 |
+
today = str(date.today())
|
| 79 |
+
|
| 80 |
+
conn = get_connection()
|
| 81 |
+
cur = conn.cursor()
|
| 82 |
+
|
| 83 |
+
cur.execute(
|
| 84 |
+
"INSERT INTO fitness_data(user_email,date,weight) VALUES (?,?,?)",
|
| 85 |
+
(email, today, weight)
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
conn.commit()
|
| 89 |
+
conn.close()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# ---------------- GET WEIGHT HISTORY ----------------
|
| 93 |
+
def get_weight_history(email):
|
| 94 |
+
|
| 95 |
+
conn = get_connection()
|
| 96 |
+
cur = conn.cursor()
|
| 97 |
+
|
| 98 |
+
cur.execute(
|
| 99 |
+
"SELECT date,weight FROM fitness_data WHERE user_email=?",
|
| 100 |
+
(email,)
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
data = cur.fetchall()
|
| 104 |
+
|
| 105 |
+
conn.close()
|
| 106 |
+
|
| 107 |
+
return data
|