-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-main.py
134 lines (110 loc) · 4.04 KB
/
contact-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
from contact import contact
import sqlite3
def add_contact(contact):
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'select * from contact where phoneno='+'\''+contact.phoneno+'\' or email='+'\''+contact.email+'\''
result = cursor.execute(sql)
result = cursor.fetchall()
if not result:
cursor.execute('insert into contact values(?,?,?)', (contact.name, contact.email, contact.phoneno))
else:
print('Already the email or phone-no exists')
db.commit()
db.close()
def ListContacts():
db = sqlite3.connect('contact.db')
cursor = db.cursor()
cursor.execute('select * from contact')
result = cursor.fetchall()
printContacts(result)
#print(result)
db.commit()
db.close()
def searchbyName(name):
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'select * from contact where name ='+'\''+name+'\''
cursor.execute(sql)
result = cursor.fetchall()
printContacts(result)
db.commit()
db.close()
def searchbyNumber(number):
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'select * from contact where phoneno ='+'\''+number+'\''
cursor.execute(sql)
result = cursor.fetchall()
printContacts(result)
db.commit()
db.close()
def printContacts(results):
print()
print('┌====================================================┐')
print('| Name \t\t Email \t\t phoneNo |')
print('|====================================================|')
for result in results:
print('| {} \t {} {} |'.format(result[0],result[1],result[2]))
print('└====================================================┘')
print()
while True:
print('\nAll Choices\n1.Add Contact\n2.Edit name using mail\n3.Edit Number using mail\n4.Search using Name\n5.Search using PhoneNumber\n6.Display All Contacts\n7.Delete\n8.Exit\n')
case_value = int(input('Enter the Option :'))
if case_value == 1:
name = input('Enter the Name:')
email = input('Enter the Email:')
phoneno = input('Enter the Number:')
contact_obj = contact(name,email,phoneno)
add_contact(contact_obj)
elif case_value == 2:
email = input('Enter the Email to edit name of contact:')
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'select * from contact where email ='+'\''+email+'\''
cursor.execute(sql)
result = cursor.fetchall()
if not result:
print('No Email Found Please check Again')
else:
name = input('Enter the Name to Update:')
sql = 'Update contact set name ='+'\''+name+'\' where email='+'\''+email+'\''
cursor.execute(sql)
db.commit()
db.close()
elif case_value == 3:
email = input('Enter the Email to edit Number:')
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'select * from contact where email ='+'\''+email+'\''
cursor.execute(sql)
result = cursor.fetchall()
if not result:
print('No Email Found Please check Again')
else:
number = input('Enter the number to Update:')
sql = 'Update contact set phoneno ='+'\''+number+'\' where email='+'\''+email+'\''
cursor.execute(sql)
db.commit()
db.close()
elif case_value == 4:
name = input('Enter the Name to Search :')
searchbyName(name)
elif case_value == 5:
number = input('Enter the Number to Search :')
searchbyNumber(number)
elif case_value == 6:
ListContacts()
elif case_value == 7:
email = input('Enter the Email to delete Contact:')
db = sqlite3.connect('contact.db')
cursor = db.cursor()
sql = 'delete from contact where email ='+'\''+email+'\''
cursor.execute(sql)
print('Deletion Success')
db.commit()
db.close()
elif case_value == 8:
exit()
else:
print("Please check The Option Entered")