-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomers.py
More file actions
115 lines (101 loc) · 3.25 KB
/
Copy pathcustomers.py
File metadata and controls
115 lines (101 loc) · 3.25 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import psycopg2
from utils import read_int, read_text
def customers_menu(cur, conn):
while True:
print("\n--- Customers ---")
print("1) List 2) Add 3) Update 4) Delete 0) Back")
ch = input("> ").strip()
if ch == "1":
customers_list(cur)
elif ch == "2":
customers_add(cur, conn)
elif ch == "3":
customers_update(cur, conn)
elif ch == "4":
customers_delete(cur, conn)
elif ch == "0":
break
else:
print("Invalid.")
def customers_list(cur):
cur.execute("SELECT id, name, email FROM customers ORDER BY id;")
rows = cur.fetchall()
if not rows:
print("No customers found.")
else:
for row in rows:
print(row)
def customers_add(cur, conn):
name = read_text("Enter name: ")
email = read_text("Enter email: ")
if not name or not email:
return
try:
cur.execute(
"INSERT INTO customers (name, email) VALUES (%s, %s) RETURNING id;", (name, email))
added = cur.fetchone()
if added:
conn.commit()
print(f"Added customer {name} & {email} with id {added['id']}")
customers_list(cur)
except psycopg2.Error as e:
conn.rollback()
print("Error adding customer:", getattr(e, "pgerror", str(e)))
def customers_update(cur, conn):
print("\nWhat do you want to update?")
print("1) Name 2) Email 0) Back")
choice = input("> ").strip()
cust_id = read_int("Enter customer id: ")
if cust_id is None:
return
try:
if choice == "1":
new_name = read_text("Enter new name: ")
if not new_name:
return
cur.execute(
"UPDATE customers SET name = %s WHERE id = %s RETURNING id;",
(new_name, cust_id)
)
elif choice == "2":
new_email = read_text("Enter new email: ")
if not new_email:
return
cur.execute(
"UPDATE customers SET email = %s WHERE id = %s RETURNING id;",
(new_email, cust_id)
)
elif choice == "0":
return
else:
print("Invalid choice.")
return
updated = cur.fetchone()
if updated:
conn.commit()
print(f"Updated customer with id {updated['id']}")
customers_list(cur)
else:
print(f"No customer found with id {cust_id}")
except psycopg2.Error as e:
conn.rollback()
print("Error updating customer:", getattr(e, "pgerror", str(e)))
def customers_delete(cur, conn):
cust_id = read_int("Enter customer id: ")
if cust_id is None:
return
try:
cur.execute(
"DELETE FROM customers WHERE id = %s RETURNING id;",
(cust_id,)
)
deleted = cur.fetchone()
if deleted:
conn.commit()
print(f"Deleted customer with id {deleted['id']}")
customers_list(cur)
else:
print(f"No customer found with id {cust_id}")
except psycopg2.Error as e:
conn.rollback()
print("Error deleting customer:", getattr(e, "pgerror", str(e)))