-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.py
124 lines (102 loc) · 3.54 KB
/
database.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
import sqlite3
from flask import g
def connect_to_database():
if 'db' not in g:
g.db = sqlite3.connect('greenAge.db')
def get_database():
if 'db' not in g:
connect_to_database()
return g.db
def close_connection():
db = g.pop('db', None)
if db is not None:
db.close()
# User table operations
def fetch_table_names():
db = get_database()
cursor = db.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
return tables
def create_user(email, name, phone, cfp, region):
db = get_database()
cursor = db.cursor()
cursor.execute('''INSERT INTO user (email, name, phone, cfp, region)
VALUES (?, ?, ?, ?, ?)''', (email, name, phone, cfp, region))
db.commit()
def fetch_users():
db = get_database()
cursor = db.cursor()
cursor.execute('''SELECT * FROM user''')
rows = cursor.fetchall()
return rows
def update_user(email, name, phone, cfp, region):
db = get_database()
cursor = db.cursor()
cursor.execute('''UPDATE user SET name=?, phone=?, cfp=?, region=? WHERE email=?''',
(name, phone, cfp, region, email))
db.commit()
def update_user_cfp(email, cfp):
db = get_database()
cursor = db.cursor()
cursor.execute('''UPDATE user SET cfp=? WHERE email=?''', (cfp, email))
db.commit()
def create_user_cfp_table(email):
db = get_database()
cursor = db.cursor()
# Safely incorporate the email into the table name
table_name = email.split('@')[0]
cursor.execute(f'''CREATE TABLE IF NOT EXISTS {table_name} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
carbon_footprint REAL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
db.commit()
def track_user_cfp(email, cfp):
db = get_database()
cursor = db.cursor()
# Safely incorporate the email into the table name
table_name = email.split('@')[0]
cursor.execute(f'''INSERT INTO {table_name} (carbon_footprint) VALUES (?)''', (cfp,))
db.commit()
def delete_user(email):
db = get_database()
cursor = db.cursor()
cursor.execute('''DELETE FROM user WHERE email=?''', (email,))
db.commit()
# Social table operations
def create_social_post(email, post, sentiment, title):
db = get_database()
cursor = db.cursor()
cursor.execute('''INSERT INTO social (email, post, sentiment, title)
VALUES (?, ?, ?, ?)''', (email, post, sentiment, title))
db.commit()
def fetch_social_posts():
db = get_database()
cursor = db.cursor()
cursor.execute('''SELECT * FROM social''')
rows = cursor.fetchall()
return rows
def update_social_post(email, post, sentiment, date):
db = get_database()
cursor = db.cursor()
cursor.execute('''UPDATE social SET post=?, sentiment=?, date=? WHERE email=?''',
(post, sentiment, date, email))
db.commit()
def delete_social_post(email, date):
db = get_database()
cursor = db.cursor()
cursor.execute('''DELETE FROM social WHERE email=? AND date=?''', (email, date))
db.commit()
def fetch_user_name(email):
db = get_database()
cursor = db.cursor()
cursor.execute('''SELECT name FROM user WHERE email=?''', (email,))
name = cursor.fetchone()
return name[0] if name else None
# Additional functions:
def fetch_table_names():
db = get_database()
cursor = db.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
return tables