-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_database.py
More file actions
92 lines (80 loc) · 2.2 KB
/
customer_database.py
File metadata and controls
92 lines (80 loc) · 2.2 KB
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
import sqlite3
class Customer:
"""
Customer class containing all the method to interact with the customer table
"""
def __init__(self, path=":memory:") -> None:
"""
Connect to in memory database by default, or to a database file if
specified.
"""
self.con = sqlite3.connect(path)
def create_table(self) -> None:
"""
Create the customer table if not exists
"""
self.con.execute(
"""
CREATE TABLE IF NOT EXISTS customer (
id INT PRIMARY KEY NOT NULL,
email TEXT NOT NULL
)
"""
)
def insert(self, customer_id: int, email: str) -> None:
"""
Insert a new customer in the table.
:param customer_id: The customer id as an int
:param email: The customer email as a string
"""
self.con.execute(
"""
INSERT INTO customer (
id,
email
) VALUES (
?, ?
)""",
(customer_id, email),
)
self.con.commit()
def get(self, customer_id: int) -> str:
"""
Get the email of a customer by its id.
:param customer_id: The customer id as an int
:return: The customer email as a string
"""
cursor = self.con.execute(
"""
SELECT email
FROM customer
WHERE id = ?
""",
(customer_id,),
)
return cursor.fetchone()[0]
def delete(self, customer_id: int) -> None:
"""
Delete a customer by its id.
:param customer_id: The customer id as an int
"""
self.con.execute(
"""
DELETE FROM customer
WHERE id = ?
""",
(customer_id,),
)
self.con.commit()
def customers(self) -> list:
"""
Get the list of all customers.
:return: The list of all customers
"""
cursor = self.con.execute(
"""
SELECT id, email
FROM customer
"""
)
return cursor.fetchall()