Springboardmen commited on
Commit
e8cc882
·
verified ·
1 Parent(s): fd35642

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +7 -23
database.py CHANGED
@@ -1,20 +1,19 @@
1
  import sqlite3
2
  from datetime import date
 
 
 
3
 
4
 
5
- # ---------------- DB CONNECTION ----------------
6
  def get_connection():
7
- conn = sqlite3.connect("users.db", check_same_thread=False)
8
  return conn
9
 
10
 
11
- # ---------------- CREATE TABLES ----------------
12
  def create_tables():
13
-
14
  conn = get_connection()
15
  cur = conn.cursor()
16
 
17
- # users table
18
  cur.execute("""
19
  CREATE TABLE IF NOT EXISTS users(
20
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -24,7 +23,6 @@ def create_tables():
24
  )
25
  """)
26
 
27
- # weight tracking table
28
  cur.execute("""
29
  CREATE TABLE IF NOT EXISTS weight_history(
30
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -38,9 +36,7 @@ def create_tables():
38
  conn.close()
39
 
40
 
41
- # ---------------- REGISTER USER ----------------
42
  def register_user(email, password):
43
-
44
  conn = get_connection()
45
  cur = conn.cursor()
46
 
@@ -51,17 +47,13 @@ def register_user(email, password):
51
  )
52
  conn.commit()
53
  return True
54
-
55
  except:
56
  return False
57
-
58
  finally:
59
  conn.close()
60
 
61
 
62
- # ---------------- LOGIN USER ----------------
63
  def login_user_db(email, password):
64
-
65
  conn = get_connection()
66
  cur = conn.cursor()
67
 
@@ -77,9 +69,7 @@ def login_user_db(email, password):
77
  return user
78
 
79
 
80
- # ---------------- SAVE OTP ----------------
81
  def save_otp(email, otp):
82
-
83
  conn = get_connection()
84
  cur = conn.cursor()
85
 
@@ -92,9 +82,7 @@ def save_otp(email, otp):
92
  conn.close()
93
 
94
 
95
- # ---------------- VERIFY OTP ----------------
96
  def verify_otp_db(email, otp):
97
-
98
  conn = get_connection()
99
  cur = conn.cursor()
100
 
@@ -110,9 +98,7 @@ def verify_otp_db(email, otp):
110
  return user
111
 
112
 
113
- # ---------------- SAVE WEIGHT ----------------
114
  def save_weight(email, weight):
115
-
116
  today = str(date.today())
117
 
118
  conn = get_connection()
@@ -127,19 +113,17 @@ def save_weight(email, weight):
127
  conn.close()
128
 
129
 
130
- # ---------------- GET WEIGHT HISTORY ----------------
131
  def get_weight_history(email):
132
-
133
  conn = get_connection()
134
  cur = conn.cursor()
135
 
136
  cur.execute(
137
- "SELECT date, weight FROM weight_history WHERE email=?",
138
  (email,)
139
  )
140
 
141
- data = cur.fetchall()
142
 
143
  conn.close()
144
 
145
- return data
 
1
  import sqlite3
2
  from datetime import date
3
+ import os
4
+
5
+ DB_FILE = "users.db"
6
 
7
 
 
8
  def get_connection():
9
+ conn = sqlite3.connect(DB_FILE, check_same_thread=False)
10
  return conn
11
 
12
 
 
13
  def create_tables():
 
14
  conn = get_connection()
15
  cur = conn.cursor()
16
 
 
17
  cur.execute("""
18
  CREATE TABLE IF NOT EXISTS users(
19
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
23
  )
24
  """)
25
 
 
26
  cur.execute("""
27
  CREATE TABLE IF NOT EXISTS weight_history(
28
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
36
  conn.close()
37
 
38
 
 
39
  def register_user(email, password):
 
40
  conn = get_connection()
41
  cur = conn.cursor()
42
 
 
47
  )
48
  conn.commit()
49
  return True
 
50
  except:
51
  return False
 
52
  finally:
53
  conn.close()
54
 
55
 
 
56
  def login_user_db(email, password):
 
57
  conn = get_connection()
58
  cur = conn.cursor()
59
 
 
69
  return user
70
 
71
 
 
72
  def save_otp(email, otp):
 
73
  conn = get_connection()
74
  cur = conn.cursor()
75
 
 
82
  conn.close()
83
 
84
 
 
85
  def verify_otp_db(email, otp):
 
86
  conn = get_connection()
87
  cur = conn.cursor()
88
 
 
98
  return user
99
 
100
 
 
101
  def save_weight(email, weight):
 
102
  today = str(date.today())
103
 
104
  conn = get_connection()
 
113
  conn.close()
114
 
115
 
 
116
  def get_weight_history(email):
 
117
  conn = get_connection()
118
  cur = conn.cursor()
119
 
120
  cur.execute(
121
+ "SELECT date,weight FROM weight_history WHERE email=?",
122
  (email,)
123
  )
124
 
125
+ history = cur.fetchall()
126
 
127
  conn.close()
128
 
129
+ return history