You are on page 1of 2

def randommatch(self):

selected_sport = self.cbsport.currentText()
date = self.dateEdit.date().toString("yyyy-MM-dd")
time = self.timeEdit.time().toString("hh:mm")
remaining_teams = self.teamnum.text()
db_host = 'localhost'
db_name = 'sport'
db_user = 'postgres'
db_password = '123456'

try:
conn = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password
)

cursor = conn.cursor()

# Fetch teams specific to the selected sport from the database


teams_query = "SELECT TEAM_NAME FROM TEAM WHERE TEAM_LEAGUE = %s"
cursor.execute(teams_query, (selected_sport,))
teams_result = cursor.fetchall()
teams = [team[0] for team in teams_result]
if not teams:
# No teams available for the selected sport
QMessageBox.warning(self, "No Teams", "No teams available for the
selected sport.")
return

if self.sender() == self.savebtn_2:
# Save button clicked, perform save functionality
home_team = self.home.currentText()
against_team = self.against.currentText()
location = self.cblocation.currentText()

sched = random.randint(1111, 1999)


match_des = f"{home_team} vs {against_team}"

while True:
cursor.execute("SELECT 1 FROM SCHEDULE WHERE SCHED_ID = %s",
(sched,))
if cursor.fetchone() is None:
break
sched = random.randint(1111, 1999)

querysched = """INSERT INTO SCHEDULE (SCHED_ID, SCHED_GAMEDATE,


SCHED_GAMETIME, SCHED_LOCVENUE)
VALUES (%s, %s, %s, %s)"""
cursor.execute(querysched, (sched, date, time, location))

# Fetch the TEAM_ID based on the home_team and against_team from


the TEAM table
team_id_query = "SELECT TEAM_ID FROM TEAM WHERE TEAM_NAME = %s"
cursor.execute(team_id_query, (home_team,))
home_team_id = cursor.fetchone()[0]
cursor.execute(team_id_query, (against_team,))
against_team_id = cursor.fetchone()[0]
res_match = random.randint(999, 9999)
queryres = "INSERT INTO RESULT (RES_ID, RES_SCORE, RES_DESCRIPTION)
VALUES (%s, %s, %s)"
cursor.execute(queryres, (res_match, 0, 0))

# Insert data into the MATCH table with the retrieved TEAM_IDs
querymatch = "INSERT INTO MATCH (MATCH_ID, MATCH_GAME_TYPE,
MATCH_STATUS, TEAM_ID, MATCH_DESCRIPTION, SCHED_ID, RES_ID) VALUES (%s, %s, %s, %s,
%s, %s, %s)"
cursor.execute(querymatch,
(res_match, selected_sport, 'Upcoming',
home_team_id, match_des, sched, res_match))

# Remove the selected teams from the options in the ComboBoxes


teams = [team for team in teams if
team.strip().lower() != home_team.strip().lower() and
team.strip().lower() != against_team.strip().lower()]
self.updateTeamOptions(teams)

querynumber = """
SELECT COUNT(TEAM_ID)
FROM TEAM
WHERE TEAM_LEAGUE = %s
AND TEAM_ID NOT IN (
SELECT TEAM_ID
FROM MATCH
WHERE MATCH_ID IS NOT NULL
AND MATCH_GAME_TYPE = %s
)
"""
cursor.execute(querynumber, (selected_sport, selected_sport))
remaining_teams = cursor.fetchone()[0]
conn.commit()
conn.close()

if remaining_teams == 0:
QMessageBox.warning(self, "No Teams", "No teams available for
matches.")
else:
# Update the remaining teams count in the GUI
self.teamnum.setText(str(remaining_teams))

except (Exception, psycopg2.Error) as error:


print("Error while connecting to PostgreSQL:", error)

You might also like