1+ from prettytable import PrettyTable
2+ import sqlite3
3+ import os
4+ os .chdir ('E:\project' )
5+ #--------------------------------------------------
6+ my_database = sqlite3 .connect ('contact.db' )
7+ try :
8+ my_database .execute ('select * from contact' )
9+ except :
10+ my_database .execute ('''CREATE TABLE CONTACT
11+ (NAME char(30) primary key NOT NULL,
12+ Phone_no INT NOT NULL,
13+ ADDRESS CHAR(50),
14+ EMAIL_ID CHAR(50));''' )
15+ #--------------------------------------------------------
16+ #print(my_database.execute('select * from contact'))
17+ class contacts :
18+ Name = str ()
19+ Mobile_no = str ()
20+ Address = str ()
21+ Email = str ()
22+ def __init__ (self ):#constructor used for declaring variable
23+ self .Name = ''
24+ self .Mobile_no = ''
25+ self .Address = ''
26+ self .Email = ''
27+ def show_table_format (self ,contact_detail ):
28+ myview = PrettyTable (['Name' ,'Phone no.' ,'Address' ,'Email Id' ])
29+ data = []
30+ for i in contact_detail :
31+ data .append (i )
32+ if (not data ):
33+ print ('oops no data found !!! :(' )
34+ return
35+ myview .add_rows (data )
36+ print (myview )
37+ return
38+
39+ def Add_contact (self ):
40+ self .Name = input ('Enter the name: ' )
41+ self .Mobile_no = input ('Enter the number: ' )
42+ self .Address = input ('Enter the address: ' )
43+ self .Email = input ('Enter the email: ' )
44+
45+ my_database .execute ('Insert into contact values("{}","{}","{}","{}")' .format (self .Name ,self .Mobile_no ,self .Address ,self .Email ))
46+ my_database .commit ()
47+ print ('Data saved succesfully' )
48+ return
49+
50+ def show_contacts (self ):
51+ contact_detail = my_database .execute ('select * from contact' )
52+ self .show_table_format (contact_detail )
53+
54+ def Edit_contacts (self ):
55+ self .Delete_contacts ()
56+ self .Add_contact ()
57+ def Delete_contacts (self ):
58+ delete_name = input ('Enter the name of contact to edit/delete: ' )
59+
60+ my_database .execute ('Delete from contact where NAME = "{}" COLLATE NOCASE' .format (delete_name ))
61+ my_database .commit ()
62+ print ('Data deleted succefully' )
63+
64+ def search_contacts (self ):
65+ search_name = input ('Enter the name of contact to search: ' )
66+ data = my_database .execute ("select * from contact where name = '{}' COLLATE NOCASE" .format (search_name ))
67+ self .show_table_format (data )
68+
69+ def start_up ():
70+ print (' ' * 15 ,'1. Press a to add new contact' )
71+ print (' ' * 15 ,'2. Press s to show contacts' )
72+ print (' ' * 15 ,'3. Press e to edit contacts' )
73+ print (' ' * 15 ,'4. Press d to delete contacts' )
74+ print (' ' * 15 ,'5. Press g to search contacts' )
75+
76+ if __name__ == "__main__" :
77+ person = contacts ()
78+ print ('----------------:Welcome to contact list management system:-------------' )
79+
80+ answer = 'y'
81+ while answer in ['y' ,'Y' ]:
82+ start_up ()
83+ choice = input ('Enter your choice: ' )
84+ if choice in ['a' ,'A' ]:
85+ person .Add_contact ()
86+ elif choice in ['s' ,'S' ]:
87+ person .show_contacts ()
88+ elif choice in ['e' ,'E' ]:
89+ person .Edit_contacts ()
90+ elif choice in ['d' ,'D' ]:
91+ person .Delete_contacts ()
92+ elif choice in ['g' ,'G' ]:
93+ person .search_contacts ()
94+ else :
95+ print ('oops invalid choice !!! ' )
96+ answer = input ('Want to perform more operation y/n !!' )
97+ print ('Programme closed succesfully' )
98+
99+
100+
101+
102+
0 commit comments