-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcovidsqlbase.py
111 lines (85 loc) · 3.49 KB
/
covidsqlbase.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
import sqlite3
from sqlite3 import Error
import os
from parameters import PATH_BASE
class CovidDataBase():
def __init__(self):
self.con = sqlite3.connect(PATH_BASE)
self.con.execute("PRAGMA foreign_keys = OFF")
self.cursor = self.con.cursor()
self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.con.close()
def close(self):
self.con.close()
###############
#### USERS ####
###############-
def addUser(self,id):
self.cursor.execute(f"INSERT INTO users(id) VALUES({id})")
self.con.commit()
#return true if len(self.getUser(id)) >0 else False
def removeUser(self,id):
try:
self.cursor.execute(f"DELETE FROM users WHERE id ={id}")
self.con.commit()
except Error:
print(Error)
def getUsers(self):
self.cursor.execute("SELECT * FROM users")
return self.cursor.fetchall()
def getUser(self,id):
self.cursor.execute(f"SELECT * FROM users WHERE id={id}")
return self.cursor.fetchall()
###############
### comunas ###
###############
def addComuna(self,name):
self.cursor.execute(f'INSERT INTO comunas(nombre) VALUES("{name}")')
self.con.commit()
def removeComuna(self,id):
try:
self.cursor.execute(f"DELETE FROM comunas WHERE id={id}" )
self.con.commit()
except Error:
print(Error)
def getComunas(self):
self.cursor.execute(f"SELECT * FROM comunas")
return self.cursor.fetchall()
################################
### suscripciones a datosEPI ###
################################
def addSuscriptionoOption(self,id,name):
self.cursor.execute(f'INSERT INTO datosEPI(id,nombre) VALUES({id},"{name}")')
self.con.commit()
def removeSuscriptionOption(self,id):
self.cursor.execute(f"DELETE FROM datosEPI WHERE id={id}")
self.con.commit()
def getSuscriptionsOptions(self):
self.cursor.execute(f"SELECT * FROM datosEPI")
return self.cursor.fetchall()
#### create suscriptions Comunas
def suscribe2Comuna(self,user_id,nombre_comuna,fase):
self.cursor.execute(f'INSERT INTO users_comunas_fases(user_id,nombre_comuna,fase) VALUES({user_id},"{nombre_comuna}",{fase})')
self.con.commit()
def unsuscribeComuna(self,user_id,nombre_comuna):
self.cursor.execute(f'DELETE FROM users_comunas_fases WHERE user_id={user_id} AND nombre_comuna="{nombre_comuna}"')
self.con.commit()
def getUserComunas(self,user_id):
self.cursor.execute(f'SELECT nombre_comuna FROM users_comunas_fases WHERE user_id={user_id}')
return self.cursor.fetchall()
#### create suscriptions datosEPI
def suscribe2Dato(self,user_id,nombre_dato):
self.cursor.execute(f'INSERT INTO user_datos(user_id,nombre_dato) VALUES({user_id},"{nombre_dato}")')
self.con.commit()
def unsuscribeDato(self,user_id,nombre_dato):
self.cursor.execute(f'DELETE FROM user_datos WHERE user_id={user_id} AND nombre_dato="{nombre_dato}"')
self.con.commit()
def getUserDatos(self,user_id):
self.cursor.execute(f'SELECT nombre_dato FROM user_datos WHERE user_id={user_id}')
return self.cursor.fetchall()
if __name__ == "__main__":
os.system('clear')
db = CovidDataBase()