-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
76 lines (66 loc) · 2.61 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
from MySQLdb import connect, cursors
import uuid
import os
class Database:
def __init__(self):
self.database_host = os.getenv("DATABASE_HOST") or "localhost"
self.database_user = os.getenv("DATABASE_USER") or "root"
self.database_password = os.getenv("DATABASE_PASSWORD") or ""
self.database_name = os.getenv("DATABASE_NAME") or "celiyfede"
self.con = None
def _connect(self):
self.con = connect(host=self.database_host, user=self.database_user, passwd=self.database_password,
db=self.database_name, cursorclass=cursors.DictCursor)
return self.con.cursor()
def _disconnect(self):
self.con.close()
def get_person_details(self, person_id):
cur = self._connect()
cur.execute("SELECT id, firstName, lastName, email, howmany, message FROM rsvp WHERE id = %s", (person_id,))
person_details = cur.fetchone()
self._disconnect()
return person_details
def get_all_people(self):
cur = self._connect()
cur.execute("SELECT id, firstName, lastName, email, howmany, message FROM rsvp")
people = cur.fetchall()
self._disconnect()
return people
def insert_person(self, **person_details):
try:
print(person_details)
person_id = uuid.uuid4()
cur = self._connect()
cur.execute(
"INSERT INTO rsvp (id, firstName, lastName, email, howmany, message) VALUES (%s,%s,%s,%s,%s,%s)",
(str(person_id),
person_details['first_name'],
person_details['last_name'],
person_details['email'],
person_details['howmany'],
person_details['message'],
))
self.con.commit()
return str(person_id)
except Exception as e:
print(e)
self.con.rollback()
finally:
self._disconnect()
def edit_person(self, person_id, **person_details):
try:
cur = self._connect()
cur.execute(
"UPDATE rsvp SET firstName = %s , lastName = %s, email = %s, howmany =%s , message = %s WHERE id = %s;",
(person_details['first_name'],
person_details['last_name'],
person_details['email'],
person_details['howmany'],
person_details['message'],
person_id))
self.con.commit()
except Exception as e:
self.con.rollback()
print(e)
finally:
self._disconnect()