-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDB1.py
141 lines (121 loc) · 4.49 KB
/
MyDB1.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
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
import MySQLdb
def database():
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
Db = db
cursor.execute("CREATE DATABASE IF NOT EXISTS {} ".format(Db))
cursor.execute("USE {}".format(Db))
cursor.execute("""CREATE TABLE IF NOT EXISTS tables
(
idnum char primary key,
course char(20),
Fname char(20),
Mname char(20),
Lname char(20)
)
""")
cursor.close()
db.close()
class Student:
def __init__(self,idnum,course,Fname,Mname,Lname):
self.idnum = idnum
self.course = course
self.Fname = Fname
self.Mname = Mname
self.Lname = Lname
def add(self):
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
sql = ("""INSERT INTO tables(idnum,course,Fname,Mname,Lname)
VALUES('%s','%s','%s','%s','%s')""" %\
(self.idnum,self.course,self.Fname,self.Mname,self.Lname))
cursor.execute(sql)
db.commit()
cursor.close()
db.close()
def delete(idnum):
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
cursor.execute("DELETE FROM tables WHERE idnum = %s",(idnum))
db.commit()
cursor.close()
db.close()
def search(idnum):
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
cursor.execute("SELECT * FROM tables WHERE idnum = %s",(idnum))
result = cursor.fetchall()
return result
def update(choice,idnum):
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
if choice == "1":
newidnum = raw_input("Enter the new idnumber: ")
cursor.execute("""UPDATE tables SET idnum = %s WHERE idnum = %s""",(newidnum,idnum))
db.commit()
elif choice == "2":
newcourse = raw_input("Enter the new course: ")
cursor.execute("UPDATE tables SET course = %s WHERE idnum = %s""",(newcourse,idnum))
db.commit()
cursor.close()
db.close()
elif choice == "3":
newFirstName = raw_input("Enter the new firstname: ")
cursor.execute("UPDATE tables SET Fname = %s WHERE idnum = %s""",(newFirstName,idnum))
db.commit()
cursor.close()
db.close()
elif choice == "4":
newMiddleName = raw_input("Enter the new middlename: ")
cursor.execute("UPDATE tables SET Mname = %s WHERE idnum = %s""",(newMiddleName,idnum))
db.commit()
cursor.close()
db.close()
elif choice == "5":
newLastName = raw_input("Enter the new lastname: ")
cursor.execute("UPDATE tables SET Lname = %s WHERE idnum = %s""",(newLastName,idnum))
db.commit()
cursor.close()
db.close()
else:
print("not in the choices\n")
def prints():
db = MySQLdb.connect(host="localhost", user="root", passwd= "", db = "dbdemo")
cursor = db.cursor()
cursor.execute('SELECT * FROM tables ORDER BY idnum ASC')
for row in cursor.fetchall():
print row
cursor.close()
db.close()
def menu():
print('What do you want to do?\n 1.Add\n 2.Delete\n 3.Search\n 4.Update\n 5.List of Students\n')
while(True):
menu()
choice1 = input("Enter your chosen number: \n")
if choice1 == 1:
idnum = raw_input("Enter idNumber: ")
course = raw_input("Enter course: ")
Fname = raw_input("Enter first name: ")
Mname = raw_input("Enter middle name: ")
Lname = raw_input("Enter last name: ")
StudInfo = Student(idnum,course,Fname,Mname,Lname)
StudInfo.add()
elif choice1 == 2:
Search = raw_input ("Enter the idnumber of the data to be deleted: \n")
delete(Search)
elif choice1 == 3:
Search = raw_input ("Enter the idnumber: \n")
print search(Search)
elif choice1 == 4:
choice = raw_input ("What do you want to update?: \n 1. Idnumber \n 2. Course \n 3. First name \n 4. Middle name \n 5. Last name \n ----> ")
idnum = raw_input ("Idnumber of the student you want to update: \n")
update(choice,idnum)
elif choice1 == 5:
prints()
else:
print("not in the choices\n")
choice2 = raw_input("Want to try again? yes or no: \n")
if (choice2 == "no"):
break
else :
print("not in the choices\n")