-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path22. Database Connectivity - Menu Based Program.py
More file actions
112 lines (89 loc) · 3.07 KB
/
22. Database Connectivity - Menu Based Program.py
File metadata and controls
112 lines (89 loc) · 3.07 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
'''
Write a menu-based program to perform the following operations on game table of Test database.
1. Insert record through user set of values.
2. Display the record of game table as per user choice of game.
3. Modify the prizeMoney and scheduleDate for any game according to user choice.
4. Delete a record of any gcode according to user choice.
Assume the following structure of game table:-
+--------------------+-----------+------+
| Field | Data Type | Size |
+--------------------+-----------+------+
| Gcode(Primary Key) | INT | 04 |
| GameName | VARCHAR | 30 |
| PrizeMoney | FLOAT | |
| ScheduleDate | DATE | |
+--------------------+-----------+------+
'''
#############################################################################
import mysql.connector as mc
def insert():
cur = con.cursor()
Gcode = int(input('Enter Game Code: '))
GameName = input('Enter Game Name: ')
PrizeMoney = float(input('Enter Prize Money: '))
ScheduleDate = input('Enter Schedule Date: ')
sql = f"INSERT INTO game VALUES ({Gcode}, '{GameName}', {PrizeMoney}, '{ScheduleDate}')"
cur.execute(sql)
con.commit()
print('Record Inserted Successfully!')
cur.close()
def display():
cur = con.cursor()
game = input('Enter Game Name to display record: ')
sql = f"SELECT * FROM game WHERE GameName = '{game}'"
cur.execute(sql)
row = cur.fetchone()
if row:
print(row)
else:
print('Record Not Found!')
cur.close()
def update():
cur = con.cursor()
game = input('Enter Game Name to update PrizeMoney and ScheduleDate: ')
sql = f"SELECT * FROM game WHERE GameName = '{game}'"
cur.execute(sql)
if cur.fetchone():
PrizeMoney = float(input('Enter new Prize Money: '))
ScheduleDate = input('Enter new Schedule Date: ')
sql = f"UPDATE game SET PrizeMoney = {PrizeMoney}, ScheduleDate = '{ScheduleDate}' WHERE GameName = '{game}'"
cur.execute(sql)
con.commit()
print('PrizeMoney and ScheduleDate Updated Successfully!')
else:
print('Record not found!')
cur.close()
def delete():
cur = con.cursor()
Gcode = int(input('Enter Game Code to delete its record: '))
sql = f"SELECT * FROM game WHERE Gcode = {Gcode}"
cur.execute(sql)
if cur.fetchone():
sql = f"DELETE FROM game WHERE Gcode = {Gcode}"
cur.execute(sql)
con.commit()
print('Record Deleted Successfully!')
else:
print('Record not found!')
cur.close()
con = mc.connect(host = 'localhost', user = 'root', password = 'password', database = 'Test')
while True:
msg = '''1. Insert record into game
2. Display record
3. Update record
4. Delete record
0. Exit'''
ch = int(input(f'{msg}\nEnter your choice: '))
if ch == 1:
insert()
elif ch == 2:
display()
elif ch == 3:
update()
elif ch == 4:
delete()
elif ch == 0:
break
else:
print('Invalid choice!')
con.close()