-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_team.py
55 lines (45 loc) · 1.73 KB
/
add_team.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
import pymysql
import redis
from mysqlcursor import cur, con
# Initialize Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def option_5():
"This is to insert Team Information into the database"
'''take the following as input
Team_ID\n
Player_ID1\n
Player_ID2\n
Player_ID3\n
Player_ID4\n
'''
try:
Team_ID = input("Enter Team ID: ")
Player_ID1 = input("Enter Player ID 1: ")
Player_ID2 = input("Enter Player ID 2: ")
Player_ID3 = input("Enter Player ID 3: ")
Player_ID4 = input("Enter Player ID 4: ")
# Check if team data exists in Redis cache
team_key = f"team:{Team_ID}"
if redis_client.exists(team_key):
print("Team data found in cache:")
team_data = redis_client.hgetall(team_key)
print({k.decode('utf-8'): v.decode('utf-8') for k, v in team_data.items()})
else:
query = "INSERT INTO Team(Team_ID, Player_ID1, Player_ID2, Player_ID3, Player_ID4) VALUES (%s, %s, %s, %s, %s)"
cur.execute(query, (Team_ID, Player_ID1, Player_ID2, Player_ID3, Player_ID4))
print("Inserted into Team table")
# Cache team data in Redis
team_data = {
'Player_ID1': Player_ID1,
'Player_ID2': Player_ID2,
'Player_ID3': Player_ID3,
'Player_ID4': Player_ID4
}
redis_client.hmset(team_key, team_data)
print("Team data cached in Redis")
con.commit()
print("Inserted into database")
except Exception as e:
con.rollback()
print("Failed to insert into database")
print(">>>>>>>>>>>>>", e)