0% found this document useful (0 votes)
11 views

Code

computet

Uploaded by

devkannan315
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Code

computet

Uploaded by

devkannan315
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 import mysql.

connector as mycon
2 conn=mycon.connect(host='localhost',user="root",password='password'
3 ,database='kannan')
4 cur=conn.cursor()
5
6 store=[]
7 cur.execute("select name from tele")
8 data=cur.fetchall()
9 for i in data:
10 store.append(i[0].lower())
11
12 def add(n):
13 print()
14 for i in range(1,n+1):
15 print("PERSON:",i)
16 a=input("Enter name:")
17
18 while True:
19 b=input("Enter phone:")
20 if b.isdigit() and len(b)==10:
21 break
22 else:
23 print("YOUR INPUT IS WRONG")
24
25 c=input("Enter place:")
26 while True:
27 d=input("Enter email id:")
28 if d[-10:]=='@gmail.com':
29 break
30 else:
31 print("YOUR INPUT IS WRONG")
32 print("try again")
33 e=input("Enter your job:")
34 q='insert into tele values(%s,%s,%s,%s,%s)'
35 f=(a,b,c,d,e)
36 cur.execute(q,f)
37 conn.commit()
38 print("The details was Successfully added to the Table")
39
40 def search():
41 print()
42 print("YOU CAN SEARCH THROUGH:")
43 print("1)Name 2)Phone")
44
45 f=input('ENTER YOUR CHOICE:')
46 while f not in ['1','2']:
47 print("Invalid input. Please enter '1' or '2'.")
48 f=input('ENTER YOUR CHOICE:')
49
50 if f=='1':
51 t=input("Enter name to be searched:")
52 a=(t,)
53 q='select * from tele where name=%s'
54 cur.execute(q,a)
55 data=cur.fetchall()
56 if cur.rowcount==0:
57 print("No contact FOUND")
58 else:
59 print("Name \t Phone \tPlace \t\t EMAIL \t\t Job")
60 for i in data:
61 print(f"{i[0]:<10} {i[1]:<12} {i[2]:<10} {i[3]:<25} {i[4]:<10}")
62
63 elif f=='2':
64 t=input("Enter phone no. to be searched:")
65 a=(t,)
66 q='select * from tele where phone=%s'
67 cur.execute(q,a)
68 data=cur.fetchall()
69 if cur.rowcount==0:
70 print("No contact FOUND")
71 else:
72 print("Name \t Phone \tPlace \t\t EMAIL \t\t Job")
73 for i in data:
74 print(f"{i[0]:<10} {i[1]:<12} {i[2]:<10} {i[3]:<25} {i[4]:<10}")
75
76
77 def remove():
78 print()
79 while True:
80 t=input("Enter name of person to be removed:")
81 if t.lower() in store:
82 break
83 else:
84 print(t,"DOES NOT EXIST")
85 a=(t,)
86 q='delete from tele where name=%s'
87 cur.execute(q,a)
88 conn.commit()
89 print("Operation successful",t,"was removed")
90
91 def edit():
92 print()
93 name=input("Enter name of the persons detail to be changed:")
94 if name.lower() in store:
95 ans=input("Do you want to change phone no (y/n) :")
96 if ans=='y':
97 while True:
98 b=input("Enter phone:")
99 if b.isdigit() and len(b)==10:
100 break
101 else:
102 print("YOUR INPUT IS WRONG")
103 q='update tele set phone=%s where name=%s'
104 u=(b,name)
105 cur.execute(q,u)
106 else:
107 pass
108 ans=input("Do you want to change place (y/n) :")
109 if ans=='y':
110 a=input("Enter new place:")
111 q='update tele set place=%s where name=%s'
112 u=(a,name)
113 cur.execute(q,u)
114 else:
115 pass
116 ans=input("Do you want to change email id (y/n) :")
117 if ans=='y':
118 a=input("Enter new emailid :")
119 q='update tele set emailid=%s where name=%s'
120 u=(a,name)
121 cur.execute(q,u)
122 else:
123 pass
124 ans=input("Do you want to change job (y/n) :")
125 if ans=='y':
126 a=input("Enter new job:")
127 q='update tele set job=%s where name=%s'
128 u=(a,name)
129 cur.execute(q,u)
130 else:
131 pass
132 conn.commit()
133 q='select * from tele where name=%s'
134 cur.execute(q,(name,))
135 print("The updated contact is:")
136 print("Name \t Phone \tPlace \t\t EMAIL \t\t Job")
137 data=cur.fetchall()
138 for i in data:
139 print(f"{i[0]:<10} {i[1]:<12} {i[2]:<10} {i[3]:<25} {i[4]:<10}")
140 if name.lower() not in store:
141 print("No contact found to edit.")
142
143 def display():
144 print()
145 print("The contacts are:")
146 print()
147 cur.execute("select * from tele")
148 data=cur.fetchall()
149 print("Name \t Phone \tPlace \t\t EMAIL \t\t Job")
150 for i in data:
151 print(f"{i[0]:<10} {i[1]:<12} {i[2]:<10} {i[3]:<25} {i[4]:<10}")
152
153 def report():
154 print("You can group the details with the following:")
155 print("1)Place 2)Job")
156 f=input('ENTER YOUR CHOICE:')
157 while f not in ['1','2']:
158 print("Invalid input. Please enter '1' or '2'.")
159 f=input('ENTER YOUR CHOICE:')
160
161 if f=='1':
162 file=open('./files/1.txt','w')
163 a=input("Enter the place:")
164 t=(a,)
165 q='select * from tele where place=%s'
166 cur.execute(q,t)
167 data=cur.fetchall()
168 file.write("Name \t\t Phone \t\t Place \t EMAIL \t\t\t\t Job")
169 file.write('\n')
170 for i in data:
171 a = "{}{:<10} {:<12} {:<10} {:<25} {:<10}"
172 sen = a.format("", i[0], i[1], i[2], i[3], i[4])
173 file.write(sen)
174 file.write('\n')
175 file.close()
176 print("The grouped data is in 1.txt")
177 if f=='2':
178 file=open('./files/1.txt','w')
179 a=input("Enter the job:")
180 t=(a,)
181 q='select * from tele where job=%s'
182 cur.execute(q,t)
183 data=cur.fetchall()
184 file.write("Name \t\t Phone \t\t Place \t EMAIL \t\t\t\t\t Job")
185 file.write('\n')
186 for i in data:
187 a = "{}{:<10} {:<12} {:<10} {:<25} {:<10}"
188 sen = a.format("", i[0], i[1], i[2], i[3], i[4])
189 file.write(sen)
190 file.write('\n')
191 file.close()
192 print("The grouped data is in 1.txt")
193
194 ans = 'yes'
195 while ans.lower() == 'yes':
196 print('''
197 1. ADD CONTACTS
198 2. SEARCH CONTACT
199 3. REMOVE CONTACT
200 4. EDIT CONTACT
201 5. DISPLAY ALL CONTACTS
202 6. CREATE REPORT
203 7. QUIT
204 ''')
205 choice = input("Enter a choice: ")
206 while choice not in "1234567":
207 print("CHOICE MUST BE IN (1,2,3,4,5,6,7)")
208 choice = input("Enter a choice: ")
209
210 if choice == "1":
211 n=int(input("Enter no of contacts to be added:"))
212 add(n)
213 elif choice == "2":
214 search()
215 elif choice == "3":
216 remove()
217 elif choice == "4":
218 edit()
219 elif choice == "5":
220 display()
221 elif choice == "6":
222 report()
223 elif choice == "7":
224 break
225 else:
226 print("Invalid input")
227 print()
228
229 ans = input("Would you like to perform another operation? (yes/no): ")
230 while ans.lower() not in ["yes", "no"]:
231 print("Invalid input. Please enter 'yes' or 'no'.")
232 ans = input("Would you like to perform another operation? (yes/no): ")

You might also like