-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_contact.py
185 lines (149 loc) · 6.72 KB
/
manage_contact.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import sqlite3
from sqlite3 import Error
from datetime import datetime
class Contacts:
def __init__(self) -> None:
'''the class responsible for database settings and communication'''
def set_up_database() -> None:
'''creates the database and table(s), if they don't already exist'''
try:
connection = sqlite3.connect('contacts.db')
cursor = connection.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT NOT NULL UNIQUE,
email TEXT,
address TEXT,
created_at NOT NULL,
updated_at NOT NULL)''')
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.commit()
connection.close()
def create_contact(self, name: str, phone: str, email: str, address: str) -> str | None:
'''creates a new contacts with the details supplied'''
current_timestamp = datetime.now().strftime('%d/%m/%Y by %H:%M:%S')
created_at = updated_at = current_timestamp
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
cursor.execute('INSERT INTO contacts (name, phone, email, address, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)', (name, phone, email, address, created_at, updated_at))
return f'New contact {name} succesfully created'
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.commit()
connection.close()
def update_contact(self, id: int, name: str, phone: str, email: str, address: str) -> str | None:
'''updates an existing contact'''
current_timestamp = datetime.now().strftime('%d/%m/%Y by %H:%M:%S')
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
if not cursor.execute('SELECT * FROM contacts WHERE id=?', (id,)).fetchone():
return
cursor.execute('UPDATE contacts SET name=?, phone=?, email=?, address=?, updated_at=? WHERE id=?', (name, phone, email, address, current_timestamp, id))
return f'Contact succesfully updated'
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.commit()
connection.close()
def get_all_contacts(self) -> list | None:
'''fetches all contacts currently saved in the database'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
contacts = cursor.execute('SELECT * FROM contacts ORDER BY name ASC').fetchall()
return contacts
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def get_single_contact(self, name: str) -> tuple | None:
'''fetches the first contact with the given name, from the database'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
contact = cursor.execute('SELECT * FROM contacts WHERE name=?', (name,)).fetchone()
if contact:
return contact
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def search_contacts(self, given_string: str) -> tuple | None:
'''returns all contact with the given string in their names'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
contacts = cursor.execute('SELECT name FROM contacts WHERE name LIKE ?', (f'%{given_string}%',)).fetchall()
if contacts:
return contacts
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def search_by_phone(self, phone: str) -> tuple | None:
'''returns all contact with the given number in their numbers'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
contacts = cursor.execute('SELECT name FROM contacts WHERE phone LIKE ?', (f'%{phone}%',)).fetchall()
if contacts:
return contacts
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def delete_contacts(self, names: tuple) -> str | None:
'''deletes the given contact(s)'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
placeholders = ', '.join(['?' for _ in names])
cursor.execute(
f"DELETE FROM contacts WHERE name IN ({placeholders})", names)
row_count = cursor.rowcount
connection.commit()
return f'No. of contacts deleted: {row_count}'
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def email_exists(self, email: str) -> bool:
'''returns True if a contact with the given email already exists'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
contact = cursor.execute('SELECT * FROM contacts WHERE email=?', (email,)).fetchone()
if not contact or contact[3] == '':
return False
return True
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()
def phone_exists(self, phone: str) -> bool | None:
'''returns True if a contact with the given phone number already exists'''
try:
connection = sqlite3.connect("contacts.db")
cursor = connection.cursor()
if cursor.execute('SELECT * FROM contacts WHERE phone=?', (phone,)).fetchone():
return True
except Error as e:
print(f'SQLite Error: {e}')
finally:
if connection:
connection.close()