-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphone_book.py
69 lines (64 loc) · 2.23 KB
/
phone_book.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
import sqlite3
con = sqlite3.connect("phone_book.db")
cursor = con.cursor()
def create_table():
cursor.execute("CREATE TABLE IF NOT EXISTS Book (S_No INTEGER ,Name TEXT,Surname TEXT,Phone_number TEXT)")
def insert_new():
name = input("Enter name :")
surname = input("Enter surname :")
phone_num = input("Enter phone number :")
cursor.execute("SELECT * FROM Book")
data = cursor.fetchall()
num = len(data)+1
cursor.execute("INSERT INTO Book (S_No,Name,Surname,Phone_number) VALUES (?,?,?,?)",(num,name,surname,phone_num))
con.commit()
def show_list():
cursor.execute("SELECT * FROM Book")
data = cursor.fetchall()
print("S.Nu: Name Surname Phone Number")
for record in data:
print(str(record).strip("()").replace(",",""))
con.commit()
def update():
show_list()
seq = int(input("Enter the 'S_No' you want to update :"))
name = input("Enter new name :")
surname = input("Enter new surname :")
phone_num = input("Enter new phone number :")
cursor.execute("""UPDATE Book SET Name = ?, Surname = ?, Phone_number = ? WHERE S_No = ? """,(name,surname,phone_num,seq))
con.commit()
def delete():
show_list()
seq = int(input("Enter the 'S_No' you want to delete :"))
cursor.execute("""DELETE FROM Book WHERE S_No = ?""",(seq, ))
cursor.execute("SELECT * FROM Book")
data = cursor.fetchall()
num = len(data)+1
for i in range(seq,num+1):
cursor.execute("""UPDATE Book SET S_No = ? WHERE S_No = ? """,(i,seq+1))
seq+=1
con.commit()
def clear_list():
cursor.execute("DELETE FROM Book ")
con.commit()
create_table()
print("\n-------------------------Welcome Your Phone Book--------------------------\n")
while True:
choice = input("Enter \n(E)--> Enter log, \n(U)--> Update log, \n(D)--> Delete log, \n(CL)--> Clear Lİst \n(SL)--> Show list \n(X)--> Exit :").lower()
if choice == "e":
insert_new()
elif choice == "u":
update()
elif choice == "d":
delete()
elif choice == "cl":
clear_list()
elif choice == "sl":
show_list()
elif choice == "x":
print("\nSee you..\n")
break
else:
print("Please enter valid entry..")
continue
con.close()