-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
57 lines (48 loc) · 1.69 KB
/
db.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
import sqlite3
from flask import g
from flask import Flask
DATABASE = 'data.db'
app=Flask(__name__)
def get_db():
print("get_db called")
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
@app.teardown_appcontext
def close_connection(exception):
print("connection closed")
db = getattr(g, '_database', None)
if db is not None:
db.close()
def register_user(email,password):
try:
cur=get_db().execute("insert into users(email,password) values(?,?)",(email,password))
get_db().commit()
return "registration success"
except sqlite3.IntegrityError as e:
return "Email already exists"
def login_user(email,password):
cur=get_db().execute("select * from users where email=? and password=?",(email,password))
results=cur.fetchone()
try:
if results[1]==email and results[2]==password:
return True
except Exception as e:
print(str(e))
return False
def get_plan_type(email):
try:
cur=get_db().execute("select plan_type from users where email=?",(email,))
results=cur.fetchone()
plan_type=results[0]
return plan_type
except Exception as e:
return "Server error: "+str(e)
def change_plan(email,plan_name):
try:
cur=get_db().execute("update users set plan_type=? where email=?",(plan_name,email))
get_db().commit()
return "Your plan has been successfully changed"
except Exception as e:
return "Server error: "+str(e)