Football Players SQL Menu
import sqlite3
database = 'instance/sqlite.db'
def schema():
conn = sqlite3.connect(database)
cursor = conn.cursor()
results = cursor.execute("PRAGMA table_info('football')").fetchall()
for row in results:
print(row)
conn.close()
schema()
import sqlite3
def read():
conn = sqlite3.connect(database)
cursor = conn.cursor()
results = cursor.execute('SELECT * FROM football').fetchall()
if len(results) == 0:
print("Table is empty")
else:
for row in results:
print(row)
cursor.close()
conn.close()
read()
import sqlite3
def create():
name = input("Enter your name:")
number = input("Enter your number:")
wins = input("Enter your wins:")
losses = input("Enter your losses:")
conn = sqlite3.connect(database)
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO football (_name, _number, _wins, _losses) VALUES (?, ?, ?, ?)", (name, number, wins, losses))
conn.commit()
print(f"A new player record {name} has been created")
except sqlite3.Error as error:
print("Error while executing the INSERT:", error)
cursor.close()
conn.close()
#create()
import sqlite3
def update():
name = input("Enter name to update")
number = input("Enter updated number")
wins = input("Enter updated wins")
losses = input("Enter updated losses")
conn = sqlite3.connect(database)
cursor = conn.cursor()
try:
cursor.execute("UPDATE football SET _number = ? WHERE _name = ?", (number, name))
cursor.execute("UPDATE football SET _wins = ? WHERE _name = ?", (wins, name))
cursor.execute("UPDATE football SET _losses = ? WHERE _name = ?", (losses, name))
if cursor.rowcount == 0:
print(f"No name {name} was not found in the table")
else:
print(f"The row with user id {name} the password has been successfully updated")
conn.commit()
except sqlite3.Error as error:
print("Error while executing the UPDATE:", error)
cursor.close()
conn.close()
#update()
import sqlite3
def delete():
name = input("Enter player name to delete")
conn = sqlite3.connect(database)
cursor = conn.cursor()
try:
cursor.execute("DELETE FROM football WHERE _name = ?", (name,))
if cursor.rowcount == 0:
print(f"No name {name} was not found in the table")
else:
print(f"The row with name {name} was successfully deleted")
conn.commit()
except sqlite3.Error as error:
print("Error while executing the DELETE:", error)
cursor.close()
conn.close()
#delete()
def menu():
operation = input("Enter: (C)reate (R)ead (U)pdate or (D)elete or (S)chema")
if operation.lower() == 'c':
create()
elif operation.lower() == 'r':
read()
elif operation.lower() == 'u':
update()
elif operation.lower() == 'd':
delete()
elif operation.lower() == 's':
schema()
elif len(operation)==0:
return
else:
print("Please enter c, r, u, or d")
menu()
try:
menu()
except:
print("Perform Jupyter 'Run All' prior to starting menu")
read()