-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
224 lines (213 loc) · 7.71 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import mysql.connector as con
from mysql.connector import Error
import sys
from datetime import date
connection = None
# Establish Connection
def connect():
global connection
try:
connection = con.connect(
host="localhost",
user="root",
password="123456",
database="Contact"
)
print("Connected!")
setup()
except Error as e:
print("The error '{}' ".format(e))
sys.exit()
# Setup & Launch
def setup():
global connection
stmt = connection.cursor()
try:
query = """
CREATE TABLE IF NOT EXISTS people (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(10) NOT NULL,
phone_no VARCHAR(10) NOT NULL,
address VARCHAR(10) NULL,
date DATE NOT NULL
);
"""
stmt.execute(query)
connection.commit()
print("SetUp Done!")
print("\n+-------------------------+")
print("| Contactfy - Contact App |")
print("+-------------------------+\n")
except Error as e:
print("Error occurred at '{}' ".format(e))
sys.exit()
finally:
stmt.close()
# Display all Contacts
def display():
global connection
print("\n+------+------------+------------+------------+------------+")
print("| %-56s |" % "ALL CONTACTS")
stmt = connection.cursor()
try:
query = "SELECT name, phone_no, address, date FROM people"
stmt.execute(query)
data = stmt.fetchall()
stmt.close()
count = 1
print("+------+------------+------------+------------+------------+")
print("| ID | Name | Phone no. | Address | Date |")
print("+------+------------+------------+------------+------------+")
for i in data:
print("| %4s | %-10s | %-9s | %-10s | %-10s |" % (count, i[0], i[1], i[2], i[3]))
count +=1
print("+------+------------+------------+------------+------------+\n")
except Error as e:
print("Error occurred at '{}' ".format(e))
sys.exit()
# Add a Contact
def add():
global connection
stmt = connection.cursor()
name = input("Enter the person name: ")
ph = input("Enter the Phone number: ")
ad = input("Enter the Address: ")
cur_date = date.today()
try:
query = "INSERT INTO people (name, phone_no, address, date) VALUES ('{}', '{}', '{}', '{}')".format(name, ph, ad, cur_date)
stmt.execute(query)
connection.commit()
print("Upload Done!\n")
except Error as e:
print("Error occurred at '{}' ".format(e))
finally:
stmt.close()
# Delete a Contact
def delete():
global connection
stmt = connection.cursor()
ele = input("Enter the 'NAME' of the Contact Which you want to Delete: ")
try:
query = "DELETE FROM people WHERE LOWER(name) = LOWER('{}')".format(ele)
stmt.execute(query)
connection.commit()
print("Deleted contact '{}' successfully!".format(ele))
except Error as e:
print("Error occurred at '{}' ".format(e))
finally:
stmt.close()
#Updating the Contacts
def update():
global connection
stmt = connection.cursor()
ele = input("Enter the 'NAME' of the Contact Which you want to Update: ")
print("+---------------------------------------+")
print("| %-37s |" % "Update Type")
print("+---------------------------------------+")
print("| Enter '1' for updating the Name |")
print("| Enter '2' for Updating the Phone no. |")
print("+---------------------------------------+")
ch = int(input("Enter your Choice: "))
print("")
if ch == 1:
try:
name = input("Enter the new Name: ")
query = "UPDATE people SET name = '{}' WHERE LOWER(name) = LOWER('{}')".format(name, ele)
stmt.execute(query)
connection.commit()
print("Updated contact '{}' successfully!".format(ele))
except Error as e:
print("Error occurred at '{}' ".format(e))
elif ch == 2:
try:
ph = input("Enter the new Phone no.: ")
query = "UPDATE people SET phone_no = '{}' WHERE LOWER(name) = LOWER('{}')".format(ph, ele)
stmt.execute(query)
connection.commit()
print("Updated contact '{}' successfully!".format(ele))
except Error as e:
print("Error occurred at '{}' ".format(e))
else:
print("Invalid Choice!")
stmt.close()
return
#Search Contacts
def search_contacts():
global connection
stmt = connection.cursor()
search_type = "name"
print("+-----------------------------------------+")
print("| %-39s |" % "Search Type")
print("+-----------------------------------------+")
print("| Enter '1' for Searching with Name |")
print("| Enter '2' for Searching with Phone no. |")
print("+-----------------------------------------+")
ch = int(input("Enter your Choice: "))
if ch == 2:
search_type = "phone_no"
ele = input("Enter the '{}' of the Contact Which you want to Search: ".format(search_type))
try:
query = f"SELECT name, phone_no, address, date FROM people WHERE LOWER({search_type}) = LOWER(%s)"
stmt.execute(query, (ele,))
data = stmt.fetchall()
stmt.close()
if not data:
print("Contact not found!")
return
count = 1
print("+------+------------+------------+------------+------------+")
print("| ID | Name | Phone no. | Address | Date |")
print("+------+------------+------------+------------+------------+")
for i in data:
print("| %4s | %-10s | %-9s | %-10s | %-10s |" % (count, i[0], i[1], i[2], i[3]))
count +=1
print("+------+------------+------------+------------+------------+\n")
except Error as e:
print("Error occurred at '{}' ".format(e))
sys.exit()
# Main Function
def main():
while True:
print("+-----------------------------------+")
print("| %-33s |" % "Menu")
print("+-----------------------------------+")
print("| Enter '1' for Adding a Contact |")
print("| Enter '2' for Updating a Contact |")
print("| Enter '3' for Searching a Contact |")
print("| Enter '4' for Deleting a Contact |")
print("| Enter '5' for Viewing All Contact |")
print("+-----------------------------------+")
ch = int(input("Enter your Choice: "))
print("")
if ch == 1:
while True:
add()
fr = input("Do you want to ENTER more Contacts ('Y' or 'N'):")
if fr.lower() == 'n':
break
elif ch == 2:
while True:
update()
fr = input("Do you want to UPDATE more Contacts ('Y' or 'N'):")
if fr.lower() == 'n':
break
elif ch == 3:
while True:
search_contacts()
fr = input("Do you want to SEARCH more Contacts ('Y' or 'N'):")
if fr.lower() == 'n':
break
elif ch == 4:
while True:
delete()
fr = input("Do you want to DELETE more Contacts ('Y' or 'N'):")
if fr.lower() == 'n':
break
elif ch == 5:
display()
else:
print("The End!")
sys.exit()
#run
connect()
main()