forked from ToniTalens/FilmsAF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistencia_pelicula_mysql.py
60 lines (52 loc) · 1.92 KB
/
persistencia_pelicula_mysql.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
#!/bin/usr/python3
from ipersistencia_pelicula import IPersistencia_pelicula
from pelicula import Pelicula
from typing import List
import mysql.connector
import logging
class Persistencia_pelicula_mysql(IPersistencia_pelicula):
def __init__(self, credencials) -> None:
self._credencials = credencials
self._conn = mysql.connector.connect(
host=credencials["host"],
user=credencials["user"],
password=credencials["password"],
database=credencials["database"]
)
if not self.check_table():
self.create_table()
def check_table(self):
try:
cursor = self._conn.cursor(buffered=True)
cursor.execute("SELECT * FROM PELICULA;")
cursor.reset()
except mysql.connector.errors.ProgrammingError:
return False
return True
def totes(self) -> List[Pelicula]:
cursor = self._conn.cursor(buffered=True)
query = "select id, titulo, anyo, puntuacion, votos from PELICULA;"
cursor.execute(query)
registres = cursor.fetchall()
cursor.reset()
resultat = []
for registre in registres:
pelicula = Pelicula(registre[1],registre[2],registre[3],registre[4],self,registre[0])
resultat.append(pelicula)
return resultat
def desa(self,pelicula:Pelicula) -> Pelicula:
pass
def llegeix(self, titol: str) -> Pelicula:
pass
if __name__ == "__main__":
logging.basicConfig(filename='pelicules.log', encoding='utf-8', level=logging.DEBUG)
credencials = {
"host" : "localhost",
"user" : "dam_app",
"password" : "1234",
"database" : "pelis"
}
pers_pelis = Persistencia_pelicula_mysql(credencials)
# print(pers_pelis.totes())
p = Pelicula("La sociedad sin nieve",2023,5.0,10000,pers_pelis)
p.persistencia.desa(p)