-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
146 lines (132 loc) · 4.21 KB
/
index.py
File metadata and controls
146 lines (132 loc) · 4.21 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
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
import sqlite3
dbase = sqlite3.connect('contacts.db')
dbase.execute('create table if not exists' +
' contact(name text,contact text)')
cur=dbase.cursor()
def welcome():
inn = input("All or New or Edit or Search or Delete: ")
return inn.lower()
def check(sname):
cur.execute(f'select name from contact where name="{sname}"')
obj = cur.fetchone()
if obj:
return True
else:
return False
def valid_no(sname):
while True:
scontact = input(f"Enter {sname}'s phone no: ")
if len(scontact)== 10 or len(scontact)== 8:
return scontact
else:
print("Number is invalid.")
def delete():
sname = input('Enter the name to be deleted: ')
sname = sname.title()
if check(sname):
cur.execute(f"delete from contact where name='{sname}'")
print("Delete successful")
else:
print(f"No contact exist named '{sname}'")
dbase.commit()
def new():
sname = input("Enter new contact name: ")
sname = sname.title()
scontact=valid_no(sname)
cur.execute('insert into contact values(?,?)',
(f'{sname}', f'{scontact}'))
print("new contact added!")
dbase.commit()
def all():
cur.execute('select * from contact')
obj = cur.fetchall()
if obj==None:
print("No contact exist!")
for a in obj:
print('name: ', a[0], ' no: ', a[1])
dbase.commit()
def update_number():
sname = input("Enter the contact name to be updated: ")
sname = sname.title()
if check(sname):
scontact = valid_no(sname)
cur.execute(f'update contact set contact="{scontact}" where name="{sname}"')
print("update successful")
else:
print(f"No contact exist named {sname}")
dbase.commit()
def update_name():
snum = input("Enter the contact number to be edited: ")
sname = input("Enter the new contact name: ")
cur.execute(f'select name from contact where contact="{snum}"')
obj=cur.fetchone()
if obj:
cur.execute(f'update contact set name="{sname.title()}" where contact="{snum}"')
print("update successful")
else:
print(f"No contact exist which has number: {snum}")
dbase.commit()
def search_number():
snum = input("Enter the contact number to be searched: ")
cur.execute(f'select name from contact where contact like "%{snum}%"')
obj = cur.fetchone()
if obj:
obj = obj[0]
print("name: ", obj)
cur.execute(f'select contact from contact where name="{obj}"')
obj = cur.fetchone()
print("contact: ", obj[0])
else:
print(f"No contact exist which has number: {snum}")
dbase.commit()
def search_name():
sname = input("Enter the contact name to be searched: ")
cur.execute(f'select name from contact where name like "%{sname}%"')
obj=cur.fetchone()
if obj:
obj = obj[0]
print("name: ",obj)
cur.execute(f'select contact from contact where name="{obj}"')
obj=cur.fetchone()
print("contact: ",obj[0])
else:
print(f"No contact exist which named: {sname}")
dbase.commit()
#main
print("WELCOME to Mobile Contact Management System")
while(True):
inn = welcome()
if inn=='new':
new()
elif inn=='all':
all()
elif inn=='edit':
while True:
i = input("Edit name or number: ")
if i=='name':
update_name()
break
elif i=='number' or i=='num':
update_number()
break
else:
print("Select from the option")
elif inn=='search':
while True:
i = input("Search name or number: ")
if i=='name':
search_name()
break
elif i=='number' or i=='num':
search_number()
break
else:
print("Select from the option")
elif inn=='delete':
delete()
elif inn=='exit':
print('VISIT AGAIN!!')
break
else:
print("Select from the option")
dbase.close()