-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession12.py
More file actions
135 lines (102 loc) · 4.55 KB
/
session12.py
File metadata and controls
135 lines (102 loc) · 4.55 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
import mysql.connector # used to connect python program to mysql database
class DBHelper:
def saveCustomerInDB(self, customer):
sql = "insert into Customer values(null, '{}', '{}', '{}')".format(customer.name, customer.phone, customer.email)
con = mysql.connector.connect(user="root", password="", host="127.0.0.1", database="AyuDB")
# we can take any name in place of con
cursor = con.cursor() #cursor is used to connect python file to my sql database
cursor.execute(sql)
con.commit() # it checks if any command between fails then it automatically fails all the commands
# and there is no change in databases
print(customer.name, " Saved !!")
def updateCustomerInDB(self, customer):
sql ="update Customer set name = '{}', email = '{}', phone = '{}' where cid = {}".format(customer.name, customer.phone, customer.email, customer.cid )
con = mysql.connector.connect(user="root", password="", host="127.0.0.1", database="AyuDB")
# we can take any name in place of con
cursor = con.cursor() #cursor is used to connect python file to my sql database
cursor.execute(sql)
con.commit() # it checks if any command between fails then it automatically fails all the commands
# and there is no change in databases
print(customer.name, " Updated !!")
def deleteCustomerInDB(self, cid):
sql ="delete from Customer where cid = {} ".format( cid )
con = mysql.connector.connect(user="root", password="", host="127.0.0.1", database="AyuDB")
# we can take any name in place of con
cursor = con.cursor() #cursor is used to connect python file to my sql database
cursor.execute(sql)
con.commit() # it checks if any command between fails then it automatically fails all the commands
# and there is no change in databases
print(cid, " Deleted !!")
def fetchAllCustomers(self):
sql = "select * from Customer order by name desc"
#sql = "select * from Customer order by name asc"
con = mysql.connector.connect(user = "root", password = "", host = "127.0.0.1", database = "AyuDB")
cursor = con.cursor()
cursor.execute(sql)
#con.commit()
"""
rows = cursor.fetchone()
print(rows)
"""
rows = cursor.fetchall()
for row in rows:
print(row) # returns list of tuples
def fetchCustomer(self, cid):
sql = "select * from Customer where cid = {}".format(cid)
con = mysql.connector.connect(user="root", password="", host="127.0.0.1", database="AyuDB")
cursor = con.cursor()
cursor.execute(sql)
rows = cursor.fetchone()
print(rows)
class Customer:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def showCustomerDetails(self):
print(">>Name: {} Phone: {} Email: {}".format(self.name, self.phone, self.email))
print("Options:")
print("1.Create New Customer")
print("2.Update Customer")
print("3.Delete Customer")
print("4.View All Customers")
print("5.Fetch only one customer")
choice = int(input())
if choice == 1:
c1 = Customer(None, None, None)
c1.name = input("Enter name:")
c1.phone = int(input("Enter phone:"))
c1.email = input("Enter email:")
c1.showCustomerDetails()
save = input("Do u like to save the customer(yes/no) ??")
if save == "yes":
db = DBHelper()
db.saveCustomerInDB(c1)
elif choice == 2:
c1 = Customer(None, None, None)
c1.cid = int(input("Enter customer ID:"))
# we need to know which customer should be updated
print("Enter new details:>>>")
c1.name = input("Enter name:")
c1.phone = input("Enter phone:")
c1.email = input("Enter email:")
c1.showCustomerDetails()
save = input("Do u like to update the customer(yes/no) ??")
if save == "yes":
db = DBHelper()
db.updateCustomerInDB(c1)
elif choice == 3:
cid = int(input("Enter cid to be deleted:")) # if a cid is deleted then it does not mean that that cid will
# be given to another customer(roll no of student is his also when he left the school . his rollno is not given
# to another student
delete = input("Do u like to delete the customer(yes/no) ??")
if delete == "yes":
db = DBHelper()
db.deleteCustomerInDB(cid)
elif choice == 4:
db = DBHelper()
db.fetchAllCustomers()
elif choice == 5:
db = DBHelper()
cid = int(input("Enter the customer id:"))
db.fetchCustomer(cid)