-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_book.py
More file actions
70 lines (62 loc) · 1.92 KB
/
contact_book.py
File metadata and controls
70 lines (62 loc) · 1.92 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
import mysql.connector
# --- Connect ---
conn = mysql.connector.connect(
host="localhost",
user="root",
password="A_ta25!wbY66@",
database="mydb"
)
cursor = conn.cursor()
# --- CREATE: Add a contact ---
def add_contact(name, phone, email):
sql = "INSERT INTO contacts (name, phone, email) VALUES (%s, %s, %s)"
cursor.execute(sql, ('Violet', 946372897, '123@gmail.com'))
conn.commit()
print(f"✅ Contact '{name}' added!")
# --- READ: Show all contacts ---
def show_contacts():
cursor.execute("SELECT * FROM contacts")
results = cursor.fetchall()
if not results:
print("No contacts found.")
for row in results:
print(f"[{row[0]}] {row[1]} | 📞 {row[2]} | ✉️ {row[3]}")
# --- UPDATE: Change a phone number ---
def update_phone(contact_id, new_phone):
sql = "UPDATE contacts SET phone = %s WHERE id = %s"
cursor.execute(sql, (new_phone, contact_id))
conn.commit()
print(f"✅ Phone updated for ID {contact_id}")
# --- DELETE: Remove a contact ---
def delete_contact(contact_id):
sql = "DELETE FROM contacts WHERE id = %s"
cursor.execute(sql, (contact_id,))
conn.commit()
print(f"🗑️ Contact ID {contact_id} deleted.")
# --- MENU ---
while True:
print("\n📒 Contact Book")
print("1. Add contact")
print("2. Show all contacts")
print("3. Update phone number")
print("4. Delete contact")
print("5. Quit")
choice = input("Choose an option: ")
if choice == "1":
n = input("Name: ")
p = input("Phone: ")
e = input("Email: ")
add_contact(n, p, e)
elif choice == "2":
show_contacts()
elif choice == "3":
i = input("Contact ID: ")
p = input("New phone: ")
update_phone(i, p)
elif choice == "4":
i = input("Contact ID to delete: ")
delete_contact(i)
elif choice == "5":
print("Goodbye!")
conn.close()
break