Spaces:
Sleeping
Sleeping
File size: 2,074 Bytes
de09f1b 42d03a8 de09f1b 42d03a8 de09f1b 42d03a8 de09f1b 42d03a8 de09f1b 42d03a8 | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import sqlite3
from datetime import date
# ---------------- DATABASE CONNECTION ----------------
def get_connection():
conn = sqlite3.connect("fitplan.db", check_same_thread=False)
return conn
# ---------------- CREATE TABLES ----------------
def create_tables():
conn = get_connection()
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE,
password TEXT,
otp TEXT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS fitness_data(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_email TEXT,
date TEXT,
weight REAL
)
""")
conn.commit()
conn.close()
# ---------------- REGISTER USER ----------------
def register_user(email, password):
conn = get_connection()
cur = conn.cursor()
try:
cur.execute(
"INSERT INTO users(email,password) VALUES (?,?)",
(email, password)
)
conn.commit()
return True
except:
return False
finally:
conn.close()
# ---------------- LOGIN USER ----------------
def login_user(email, password):
conn = get_connection()
cur = conn.cursor()
cur.execute(
"SELECT * FROM users WHERE email=? AND password=?",
(email, password)
)
user = cur.fetchone()
conn.close()
return user
# ---------------- SAVE WEIGHT ----------------
def save_weight(email, weight):
today = str(date.today())
conn = get_connection()
cur = conn.cursor()
cur.execute(
"INSERT INTO fitness_data(user_email,date,weight) VALUES (?,?,?)",
(email, today, weight)
)
conn.commit()
conn.close()
# ---------------- GET WEIGHT HISTORY ----------------
def get_weight_history(email):
conn = get_connection()
cur = conn.cursor()
cur.execute(
"SELECT date,weight FROM fitness_data WHERE user_email=?",
(email,)
)
data = cur.fetchall()
conn.close()
return data |