Untitled

mail@pastecode.io avatar
unknown
python
22 days ago
5.2 kB
0
Indexable
Never
import sqlite3
 
 
def CreateDatabase():
    database = sqlite3.connect("DriverDB.db")
    create_drivers = """
       CREATE Table IF NOT EXISTS DriverDB (
           DriverID Integer PRIMARY KEY,
           DriverName String,
           DriverNumber String,
           TeamName String,
           Wins Integer
       )"""
    create_teams = """
       CREATE Table IF NOT EXISTS TeamDB (
           TeamID Integer PRIMARY KEY,
           TeamName String,
           NumOfDrivers Integer,
           ChampionshipWins Integer
       )"""
    create_results = """
       CREATE Table IF NOT EXISTS ResultsDB (
           ResultID Integer PRIMARY KEY,
           DriverName String,
           DriverID Integer,
           Event String,
           Position Sring
       )"""
    create_users = """
       CREATE Table IF NOT EXISTS UserDB (
           UserID Integer PRIMARY KEY,
           Username String,
           HashedPassword String
       )"""
    create_sessions = """
       CREATE Table IF NOT EXISTS SessionDB (
           SessionID Integer PRIMARY KEY,
           UserID Integer,
           SessionHash String,
           SessionExpire String
       )"""
 
    database.execute(create_drivers)
    database.execute(create_teams)
    database.execute(create_results)
    database.commit()
    return database
 
  database = CreateDatabase()
   
 
def AddDriver(name, number, teamName, wins):
    database = sqlite3.connect("DriverDB.db")
    driver_id = database.execute("SELECT COUNT(*) FROM DriverDB").fetchone()[0] + 1
    database.execute("INSERT INTO DriverDB VALUES (?, ?, ?, ?, ?)", (driver_id, name, number, teamName, wins))
    database.commit()
 
def AddTeam(teamName, NumOfDrivers, ChampionshipWins):
    database = sqlite3.connect("DriverDB.db")
    team_id = database.execute("SELECT COUNT(*) FROM TeamDB").fetchone()[0] + 1
    database.execute("INSERT INTO TeamDB VALUES (?, ?, ?, ?)", (team_id, teamName, NumOfDrivers, ChampionshipWins))
    database.commit()
 
def AddResult(driverId, event, position):
    database = sqlite3.connect("DriverDB.db")
    result_id = database.execute("SELECT COUNT(*) FROM ResultsDB").fetchone()[0] + 1
    driverName = database.execute("SELECT DriverDB.DriverName FROM DriverDB WHERE DriverDB.DriverID == ?", (driverId,)).fetchone()[0]
   
    if driverName == None:
        print("Tried to insert result with an invalid driver ID ")
    else:
        database.execute("INSERT INTO ResultsDB VALUES (?, ?, ?, ?, ?)", (result_id, driverId, driverName, event, position))
        database.commit()
 
        # Read all drivers
def fetchDrivers():
    database = sqlite3.connect("DriverDB.db")
    fetchall = database.execute("SELECT * From DriverDb").fetchall()
    database.commit()
    database.close()
    return fetchall
 
def fetchTeams():
    database = sqlite3.connect("DriverDB.db")
    fetchall = database.execute("SELECT * FROM TeamDB").fetchall()
    print(fetchall)
    database.commit()
    database.close()
    return fetchall
 
def fetchResults():
    database = sqlite3.connect("DriverDB.db")
    fetchall = database.execute("SELECT * FROM ResultsDB").fetchall()
    print(fetchall)
    database.commit()
    database.close()
    return fetchall
 
 
   
 
 
def LoopTime():
    i = input(str("Please enter what action you would like to do? n If you would like to add a Driver Press D then Enter. n If you would like to add a team please press T then enter. n If you would like to add a result press R then Enter. n Please enter a value: "))
    if i.upper() == "D":
   
        name = input("What is the Drivers full name? ")
        number = input("What is the drivers number? ")
        #rank = input("What rank is the driver in the British Karting Championships? (If they didnt compete put 0) ")
        teamName = input("What is the name of the team the driver races for? ")
        wins = input("How many wins has the driver got? ")
        AddDriver(name, number, teamName, wins)
    elif i.upper() == "T":
        teamName = input("What is the name of the Team? ")
        NumOfDrivers = input("How many drivers are in the team? ")
        ChampionshipWins = input("How many championship wins does the team have? ")
        AddTeam(teamName, NumOfDrivers, ChampionshipWins)
    elif i.upper() == "R":
        drivers = database.execute("SELECT DriverName, DriverID FROM DriverDB").fetchall()
        print(f"�33[4m{'Name': >28}�33[0m �33[4m{'ID': >29}�33[0m")
       
        for _ in range(len(drivers)):
            driver = drivers[_]
            print(f"{driver[0]: >20} {driver[1]: >20}")
       
        selectDriver = input("Input the Driver ID of the driver you want to add a result for. ")
        idFetch = database.execute("SELECT DriverDB.DriverName FROM DriverDB WHERE DriverDB.DriverID == ?", (selectDriver,)).fetchone()
        idFetch = str(idFetch)
        event = input("What event is the result from? ")
        result = input("What place did the driver finish? ")
        AddResult(idFetch, selectDriver, event, result)
 
 
#teamRank = input("")
database.close()
Leave a Comment