-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
127 lines (110 loc) · 4.08 KB
/
sample.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sqlite3
from datetime import datetime
# Register datetime adapter and converter
def adapt_datetime(value):
return value.isoformat() # Convert datetime to ISO 8601 string format
def convert_datetime(value):
return datetime.fromisoformat(value) # Convert string back to datetime object
sqlite3.register_adapter(datetime, adapt_datetime)
sqlite3.register_converter("datetime", convert_datetime)
def initialize_database():
conn = sqlite3.connect('contacts.db', detect_types=sqlite3.PARSE_DECLTYPES) # Enable datetime parsing
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
number TEXT NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP,
notes TEXT
)
''')
conn.commit()
conn.close()
def add_contact():
name = input("Please enter your name: ")
number = input("Please enter your number: ")
email = input("Please enter your email: ")
notes = input("Any additional notes (optional): ")
conn = sqlite3.connect('contacts.db', detect_types=sqlite3.PARSE_DECLTYPES) # Enable datetime parsing
cursor = conn.cursor()
cursor.execute('''
INSERT INTO contacts (name, number, email, created_at, notes)
VALUES (?, ?, ?, ?, ?)
''', (name, number, email, datetime.now(), notes)) # Store current datetime
conn.commit()
conn.close()
print("Contact added successfully!")
def show_contact():
conn = sqlite3.connect('contacts.db', detect_types=sqlite3.PARSE_DECLTYPES) # Enable datetime parsing
cursor = conn.cursor()
cursor.execute('SELECT * FROM contacts')
contacts = cursor.fetchall()
if not contacts:
print("No contacts available.")
return
print("\n-------- Contacts --------")
for contact in contacts:
print(f"\nID: {contact[0]}")
print(f"Name: {contact[1]}")
print(f"Number: {contact[2]}")
print(f"Email: {contact[3]}")
print(f"Created: {contact[4]}") # This should now be a datetime object
if contact[5]: # If notes exist
print(f"Notes: {contact[5]}")
print("-" * 25)
conn.close()
def remove_contact():
show_contact() # Show contacts first
contact_id = input("\nEnter the ID of the contact to remove: ")
conn = sqlite3.connect('contacts.db', detect_types=sqlite3.PARSE_DECLTYPES) # Enable datetime parsing
cursor = conn.cursor()
cursor.execute('DELETE FROM contacts WHERE id = ?', (contact_id,))
if cursor.rowcount > 0:
print("Contact removed successfully!")
else:
print("Contact not found.")
conn.commit()
conn.close()
def search_contacts():
search_term = input("Enter name or number to search: ")
conn = sqlite3.connect('contacts.db', detect_types=sqlite3.PARSE_DECLTYPES) # Enable datetime parsing
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM contacts
WHERE name LIKE ? OR number LIKE ? OR email LIKE ?
''', (f'%{search_term}%', f'%{search_term}%', f'%{search_term}%'))
results = cursor.fetchall()
if results:
print("\n-------- Search Results --------")
for contact in results:
print(f"\nID: {contact[0]}")
print(f"Name: {contact[1]}")
print(f"Number: {contact[2]}")
print(f"Email: {contact[3]}")
else:
print("No matching contacts found.")
conn.close()
# Initialize database and start main program
initialize_database()
while True:
print("\n1. Add Contact")
print("2. Remove Contact")
print("3. Show All Contacts")
print("4. Search Contacts")
print("5. Exit")
choice = input("\nEnter your choice (1-5): ")
if choice == '1':
add_contact()
elif choice == '2':
remove_contact()
elif choice == '3':
show_contact()
elif choice == '4':
search_contacts()
elif choice == '5':
print("Program stopped.")
break
else:
print("Invalid choice, please try again.")