-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCRUD.py
186 lines (155 loc) · 7.76 KB
/
CRUD.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
import mysql.connector
import os
# NOTAS DE CÓDIGO:
# CREO QUE HAY QUE CAMBIAR, DENTRO DE LA BASE DE DATOS, "URL-IMAGEN" POR "IMAGEN"
# Y QUE EL TIPO DE DATO DEJE DE SER UN STRING Y PASE A SER BLOB PARA QUE ACETPTE FOTOGRAFÍAS DE USUARIOS
class Credenciales:
# Obtiene credenciales para operar en la Base de Datos (BdD).
# Abre y cierra la conección con la misma.
# A modo de prueba es capaz de imprimirlas también.
def __init__(self):
ruta = "RecetasWebCAC23/datos.txt"
if os.path.exists(ruta):
with open(ruta, "r") as archivo:
lineas = archivo.readlines()
self._host = lineas[0].strip()
self._user = lineas[1].strip()
self._password = lineas[2].strip()
self._database = lineas[3].strip()
archivo.close()
else:
with open ("RecetasWebCAC23\datos.txt", "w") as archivo:
print("Desea crear archivo con las credenciales para operar en la base de datos?")
conf = input("S/N")
if conf.lower() =="s":
print("Se van a guardar las credenciales")
self._host = input("host: ")
self._user = input("user: ")
self._password = input("password: ")
self._database = input("database: ")
archivo.write(f"{self.host_}\n")
archivo.write(f"{self._user}\n")
archivo.write(f"{self._password}\n")
archivo.write(f"{self._database}\n")
else:
print("Ingrese las credenciales, NO van a ser almacenadas")
self.host_ = input("host: ")
self._password = input("password: ")
self._database = input("database: ")
archivo.close()
def imprimir(self):
print("@"*10)
print(self._host)
print(self._user)
print(self._password)
print(self._database)
print("@"*10)
def conectar(self):
self.conexion = mysql.connector.connect(
host = self._host,
user = self._user,
password = self._password,
database = self._database,
)
self.cursor = self.conexion.cursor(dictionary=True)
print('Conectado')
def comitear(self):
self.conexion.commit()
print("Comiteado")
def desconectar(self):
self.conexion.close()
print("Desconectado")
#
class Consulta(Credenciales):
def __init__(self, seleccion, tabla,condicion):
super().__init__()
self.seleccion = seleccion
self.tabla = tabla
self.condicion = condicion
def consultar(self):
self.conectar()
if not self.condicion:
self.cursor.execute ("SELECT %s FROM %s" % (self.seleccion,self.tabla,))
else:
self.cursor.execute ("SELECT %s FROM %s WHERE %s" % (self.seleccion,self.tabla,self.condicion,))
self.resultados = self.cursor.fetchall()
def imprimir(self):
if not self.condicion:
print("SELECT %s FROM %s" % (self.seleccion,self.tabla,))
else:
print("SELECT %s FROM %s WHERE %s" % (self.seleccion,self.tabla,self.condicion,))
#
class CargaDatos(Credenciales):
# valores, al igual que columnas por algún motivo necesita ingresar entre "", y si por algún motivo
# Se inscribe un único valor, debe hacerse "'elemento a ingresar'"
def __init__(self,tabla, columnas, valores):
super().__init__()
self.tabla = tabla
self.columnas = columnas
self.valores = valores
def insertarDato(self):
self.conectar()
self.cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % (self.tabla, self.columnas, self.valores,))
Credenciales.comitear(self)
def imprimir(self):
print("INSERT INTO %s (%s) VALUES (%s)" % (self.tabla, self.columnas, self.valores,))
#
class ModificarDatos(Credenciales):
def __init__(self,tabla, valores,id):
super().__init__()
self.tabla = tabla
self.valores = valores
self.id = id
def insertarDato(self):
self.conectar()
if self.tabla == 'recetas':
self.cursor.execute("UPDATE recetas SET NombreReceta = '%s', Receta = '%s', Porciones = %s, urlImagen = '%s', TiempoMin = %s,idUsuario = %s, idCategoria = %s, dificultad = '%s' WHERE idReceta = %s"
% (self.valores["NombreReceta"], self.valores["Receta"], self.valores["Porciones"], self.valores["urlImagen"], self.valores["TiempoMin"], self.valores["idUsuario"], self.valores["idCategoria"], self.valores["dificultad"], self.id))
elif self.tabla == 'categorias':
self.cursor.execute("UPDATE categorias SET Categoria = '%s' WHERE idCategoria = %s" % (self.valores["Categoria"], self.id))
elif self.tabla == 'favoritos':
self.cursor.execute("UPDATE favoritos SET idReceta = %s, idUsuario = %s WHERE idFavorito = %s" % (self.valores["idReceta"], self.valores["idUsuario"], self.id))
elif self.tabla == 'usuarios':
self.cursor.execute("UPDATE usuarios SET Nombre = '%s', Apellido = '%s', Nacimiento = %s, correoElectronico = '%s', Hash = %s, Genero = '%s' WHERE idUsuario = %s"
% (self.valores["Nombre"], self.valores["Apellido"], self.valores["Nacimiento"], self.valores["correoElectronico"], self.valores["Hash"], self.valores["Genero"], self.id))
else:
print("Error, tabla ingresada, no válida")
Credenciales.comitear(self)
def imprimir(self):
if self.tabla == 'recetas':
print("UPDATE recetas SET NombreReceta = '%s', Receta = '%s', Porciones = %s, urlImagen = '%s', TiempoMin = %s,idUsuario = %s, idCategoria = %s, dificultad = '%s' WHERE idReceta = %s"
% (self.valores["NombreReceta"], self.valores["Receta"], self.valores["Porciones"], self.valores["urlImagen"], self.valores["TiempoMin"], self.valores["idUsuario"], self.valores["idCategoria"], self.valores["dificultad"], self.id))
elif self.tabla == 'categorias':
print("UPDATE categorias SET Categoria = '%s' WHERE idCategoria = %s" % (self.valores["Categoria"], self.id))
elif self.tabla == 'favoritos':
print("UPDATE favoritos SET idReceta = %s, idUsuario = %s WHERE idFavorito = %s" % (self.valores["idReceta"], self.valores["idUsuario"], self.id))
elif self.tabla == 'usuarios':
print("UPDATE usuarios SET Nombre = '%s', Apellido = '%s', Nacimiento = %s, correoElectronico = '%s', Hash = %s, Genero = '%s' WHERE idUsuario = %s"
% (self.valores["Nombre"], self.valores["Apellido"], self.valores["Nacimiento"], self.valores["correoElectronico"], self.valores["Hash"], self.valores["Genero"], self.id))
else:
print("Error, tabla ingresada, no válida")
class EliminarDatos(Credenciales):
def __init__(self, seleccion, tabla,condicion):
super().__init__()
self.seleccion = seleccion
self.tabla = tabla
self.condicion = condicion
def borrar(self):
self.conectar()
if not self.condicion:
self.cursor.execute ("DELETE FROM %s" % (self.tabla,))
else:
self.cursor.execute ("DELETE FROM %s WHERE %s" % (self.tabla,self.condicion,))
Credenciales.comitear(self)
def imprimir(self):
if not self.condicion:
print("DELETE FROM %s" % (self.tabla,))
else:
print("DELETE FROM %s WHERE %s" % (self.tabla,self.condicion,))
#
cc = Credenciales()
cc.conectar()
cons = Consulta('*','recetas',False)
cons.consultar()
print(cons.resultados)
cc.desconectar()