-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_Core.py
107 lines (93 loc) · 3.21 KB
/
Data_Core.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
import os
import mysql.connector
from typing import Tuple
def maak_verbinding_met_database() -> mysql.connector.connection.MySQLConnection:
"""Maak verbinding met de lokale database."""
verbinding = mysql.connector.connect(
host="localhost",
user="posta",
password=os.environ.get("paswoord_db"),
database="dbposta")
return verbinding
def haal_laatste_id_op() -> int:
"""Haal het laatste ID op uit de photos tabel."""
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
cursor.execute("SELECT MAX(id) FROM info;")
resultaat = cursor.fetchone()[0] or 0
cursor.close()
verbinding.close()
return resultaat
def sla_afbeelding_op(foto_locatie: str, id: int) -> None:
"""Sla de locatie van de afbeelding op in de database met behulp van het ID."""
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
sql_command = f"""
UPDATE info
SET picture = %s
WHERE id = %s;
"""
cursor.execute(sql_command, (foto_locatie, id))
verbinding.commit()
cursor.close()
verbinding.close()
def sla_beschrijving_op(beschrijving: str, id: int) -> None:
"""Sla de beschrijving op in de database met behulp van het ID."""
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
sql_command = f"""
UPDATE info
SET description = %s
WHERE id = %s;
"""
cursor.execute(sql_command, (beschrijving, id))
verbinding.commit()
cursor.close()
verbinding.close()
def afbeelding_met_beschrijving_uploaden(foto_locatie: str, beschrijving: str) -> Tuple[str, int]:
"""Upload een afbeelding samen met de beschrijving."""
id = haal_laatste_id_op() + 1
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
sql_command = f"""
INSERT INTO info (id, picture, description)
VALUES (%s, %s, %s);
"""
cursor.execute(sql_command, (id, foto_locatie, beschrijving))
verbinding.commit()
cursor.close()
verbinding.close()
return beschrijving, id
def afbeelding_beschrijving_wijzigen(id: int, foto_locatie: str = None, beschrijving: str = None) -> None:
"""Wijzig de afbeelding, de beschrijving of beide."""
if foto_locatie:
sla_afbeelding_op(foto_locatie, id)
if beschrijving:
sla_beschrijving_op(beschrijving, id)
def haal_data_op() -> Tuple[str, int]:
"""Haal de foto en beschrijving op uit de photos tabel."""
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
cursor.execute("SELECT id, picture, description FROM info;")
resultaat = cursor.fetchall()
cursor.close()
verbinding.close()
return resultaat
def verwijder_data(id: int) -> bool:
"""Verwijder de foto en beschrijving met het gegeven ID uit de database."""
verbinding = maak_verbinding_met_database()
cursor = verbinding.cursor()
sql_command = f"""
DELETE FROM info
WHERE id = %s;
"""
try:
cursor.execute(sql_command, (id,))
verbinding.commit()
cursor.close()
verbinding.close()
return True
except:
cursor.close()
verbinding.close()
return False