-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMySQL.py
More file actions
68 lines (61 loc) · 2.41 KB
/
MySQL.py
File metadata and controls
68 lines (61 loc) · 2.41 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
import mysql.connector
import sys
import json
class MySQL:
def __init__(self, DatabaseHost, DatabaseName, DatabaseUser, DatabasePass):
try:
conn = mysql.connector.connect(
host=DatabaseHost,
database=DatabaseName,
user=DatabaseUser,
password=DatabasePass
)
conn.autocommit = True
# conn.autocommit(True)
cur = conn.cursor(dictionary=True)
print(f"Connected To {DatabaseName} .")
self.cur = cur
self.conn = conn
except:
print('Error in Connecting to Database .')
sys.exit(1)
def GetMatch(self, Id):
SQLQuery = "SELECT * FROM matches WHERE id=%s"
SQLData = (Id,)
self.cur.execute(SQLQuery, SQLData)
Result = self.cur.fetchone()
if Result == None:
# It Means Match is not in database
return False
return Result
def CreateMatch(self, MatchData):
SQLQuery = "INSERT INTO matches (id, Team1Name, Team2Name, Team1Score, Team2Score, League, GoalData) VALUES(%s,%s,%s,%s,%s,%s,%s)"
SQLData = (MatchData['id'], MatchData['Team1Name'], MatchData['Team2Name'],
MatchData['Team1Score'], MatchData['Team2Score'], MatchData['League'], '[]')
self.cur.execute(SQLQuery, SQLData)
def FinishMatch(self, Id):
MatchData = self.GetMatch(Id)
if MatchData == False:
return False
GoalData = json.loads(MatchData['GoalData'])
if int(MatchData['Team1Score']) + int(MatchData['Team2Score']) == len(GoalData):
SQLQuery = "UPDATE matches SET status = 1 WHERE id = %s"
SQLData = (Id,)
self.cur.execute(SQLQuery, SQLData)
return True
def AddToGoalData(self, Id, GoalDetails):
MatchData = self.GetMatch(Id)
if MatchData == False:
return False
if GoalDetails['T'] == 1:
TeamName = 'Team1Score'
elif GoalDetails['T'] == 2:
TeamName = 'Team2Score'
GoalData = json.loads(MatchData['GoalData'])
GoalData.append(GoalDetails)
GoalData = json.dumps(GoalData)
SQLQuery = "UPDATE matches SET GoalData = %s , " + \
TeamName + " = " + TeamName + " + 1 WHERE id = %s"
SQLData = (GoalData, Id)
self.cur.execute(SQLQuery, SQLData)
return True