-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_helper.py
187 lines (144 loc) · 4.5 KB
/
database_helper.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sqlite3
from flask import g
database_uri = "database.db"
def get_db():
db = getattr(g, "db", None) # To check if there is a database or not
if db is None:
db = g.db = sqlite3.connect(database_uri)
return db
def disconnect():
db = getattr(g, "db", None)
if db is not None:
db.close()
g.db = None
def create_user(fname, lname, gender, city, country, email, password):
try:
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO user (firstname, familyname, gender, city, country, email,password) VALUES (?, ?, ?, ?, ?, ?, ?)",
(fname, lname, gender, city, country, email, password))
get_db().commit()
return True
except:
return False
def add_user_token_table(email):
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO token_data (email) VALUES (?)", (email,))
get_db().commit()
def find_user(email):
db = get_db()
cursor = db.cursor()
cursor = db.execute(
"select * from user where email like ?", [email])
data = cursor.fetchone()
# print(data)
if (data != None):
return True
else:
return False
def get_password_with_email(email):
db = get_db()
cursor = db.cursor()
cursor = db.execute(
"select password from user where email like ?", [email])
password_data = cursor.fetchone()[0]
# print(password_data)
return (password_data)
def token_store(email, token):
db = get_db()
cursor = db.cursor()
cursor.execute(
"UPDATE token_data SET token = ? WHERE email = ?", (token, email))
get_db().commit()
def get_token(token):
db = get_db()
cursor = db.cursor()
cursor.execute("select * from token_data where token like ?", (token,))
token_value = cursor.fetchone()
# print(token_value)
if (token_value != None):
return True
else:
return False
def remove_token(token):
db = get_db()
cursor = db.cursor()
cursor.execute(
# if it is () with one value then we need to put comma, it we use [] then we dont need to put comma
"update token_data set token = NULL where token = ?", (token,))
get_db().commit()
def get_email(token):
db = get_db()
cursor = db.cursor()
cursor.execute("select email from token_data where token like ?", [token])
email_value = cursor.fetchone()[0]
return (email_value)
def update_password(password, email):
db = get_db()
cursor = db.cursor()
cursor.execute("update user set password = ? where email = ?", [
password, email])
get_db().commit()
def append_message(sender_email, receiver_email, message, latitude, longitude):
db = get_db()
cursor = db.cursor()
cursor.execute(
"""
INSERT INTO
messages (sender, receiver, message_data,latitude,longitude)
VALUES
(?, ?, ?,?,?)
""",
[sender_email, receiver_email, message, latitude, longitude]
)
db.commit()
def get_messages(receiver_email):
db = get_db()
cursor = db.cursor()
all_msgs = cursor.execute(
"SELECT sender, receiver, message_data,latitude, longitude FROM messages where receiver = ?", [receiver_email])
all_msgs = all_msgs.fetchall()
return all_msgs
def get_user_data_with_token(token): # get user data with token from database
db = get_db()
cursor = db.cursor() # sqlite internal function to execute the query
cursor.execute(
"""
select
firstname,
familyname,
gender,
city,
country,
email
from
user
where
email = (select email from token_data where token = ?)
""",
[token])
# fetchone returns None if there is no data
user_data = cursor.fetchone() # just to get one row from the database.to be safe
return user_data
def get_user_data_with_email(email): # get user data with email from database
db = get_db()
cursor = db.cursor() # sqlite internal function to execute the query
cursor.execute(
"""
select
firstname,
familyname,
gender,
city,
country,
email
from
user
where
email = ?
""",
[email])
# fetchone returns None if there is no data
user_data = cursor.fetchone() # just to get one row from the database.to be safe
return user_data