Springboardmen commited on
Commit
05ce217
·
verified ·
1 Parent(s): 9e8cd06

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +11 -46
database.py CHANGED
@@ -1,14 +1,11 @@
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()
@@ -22,22 +19,12 @@ def create_tables():
22
  )
23
  """)
24
 
25
- cur.execute("""
26
- CREATE TABLE IF NOT EXISTS fitness_data(
27
- id INTEGER PRIMARY KEY AUTOINCREMENT,
28
- user_email TEXT,
29
- date TEXT,
30
- weight REAL
31
- )
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
 
@@ -54,54 +41,32 @@ def register_user(email, password):
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
 
1
  import sqlite3
2
  from datetime import date
3
 
 
 
4
  def get_connection():
5
  conn = sqlite3.connect("fitplan.db", check_same_thread=False)
6
  return conn
7
 
8
 
 
9
  def create_tables():
10
  conn = get_connection()
11
  cur = conn.cursor()
 
19
  )
20
  """)
21
 
 
 
 
 
 
 
 
 
 
22
  conn.commit()
23
  conn.close()
24
 
25
 
26
+ # Register user
27
  def register_user(email, password):
 
28
  conn = get_connection()
29
  cur = conn.cursor()
30
 
 
41
  conn.close()
42
 
43
 
44
+ # Save OTP
45
+ def save_otp(email, otp):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  conn = get_connection()
47
  cur = conn.cursor()
48
 
49
  cur.execute(
50
+ "UPDATE users SET otp=? WHERE email=?",
51
+ (otp, email)
52
  )
53
 
54
  conn.commit()
55
  conn.close()
56
 
57
 
58
+ # Verify OTP
59
+ def verify_otp(email, otp):
 
60
  conn = get_connection()
61
  cur = conn.cursor()
62
 
63
  cur.execute(
64
+ "SELECT * FROM users WHERE email=? AND otp=?",
65
+ (email, otp)
66
  )
67
 
68
+ user = cur.fetchone()
69
 
70
  conn.close()
71
 
72
+ return user