2.4 Hacks
import sqlite3
def create_connection(db):
connection = None
try:
connection = sqlite3.connect(db)
return connection
except Error as e:
print(e)
return connection
def create_table(connection, createTblSql):
try:
cursor = connection.cursor()
cursor.execute(createTblSql)
except Error as e:
print(e)
def main():
database = 'instance/basketball.db'
createTblSql = """ CREATE TABLE IF NOT EXISTS basketball (
_id integer PRIMARY KEY,
_player text NOT NULL,
_team text NOT NULL,
_position text NOT NULL,
_opinion text NOT NULL
); """
connection = create_connection(database)
# create basketball table
if connection is not None:
create_table(connection, createTblSql)
else:
print('Connection Error')
if __name__ == '__main__':
main()
def menu():
operation = input("Enter: (C)reate (R)ead (U)pdate or (D)elete")
if operation.lower() == 'c':
create()
elif operation.lower() == 'r':
read()
elif operation.lower() == 'u':
update()
elif operation.lower() == 'd':
delete()
elif len(operation)==0: # Escape Key
return
else:
print("Please enter c, r, u, or d")
menu() # recursion, repeat menu
try:
menu() # start menu
except:
print("Perform Jupyter 'Run All' prior to starting menu")
def create():
database = 'instance/basketball.db'
player = input("Enter the player name")
team = input("Enter team name")
position = input("Enter player position")
opinion = input("Enter your opinion ")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
# Execute SQL to insert record in to db
cursor.execute("INSERT INTO basketball (_player, _team, _position, _opinion) VALUES (?, ?, ?, ?)", (player, team, position, opinion))
# Commit the changes
connection.commit()
print(f"New basketball with {player} is added.")
except sqlite3.Error as error:
print("Error while inserting record", error)
# Closing cursor and connection
cursor.close()
connection.close()
create()
def read():
database = 'instance/basketball.db'
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
# Fetch all the records from basketball table
results = cursor.execute('SELECT * FROM basketball').fetchall()
if len(results) != 0:
for row in results:
print(row)
else:
print("No basketball")
# Closing cursor and connection
cursor.close()
connection.close()
read()
import sqlite3
# updating review
def update():
database = 'instance/basketball.db'
basketballId = input("Enter a basketball id to update the review")
opinion = input("Enter new opinion")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
# Updating review for the basketball
cursor.execute("UPDATE basketball SET _opinion = ? WHERE _id = ?", (opinion, basketballId))
if cursor.rowcount != 0:
print(f"Review for the basketball is updated to {opinion}")
connection.commit()
else:
print(f"basketball not found")
except sqlite3.Error as error:
print("Error occurred", error)
# Closing cursor and connection
cursor.close()
connection.close()
update()
import sqlite3
def delete():
database = 'instance/basketball.db'
basketballId = input("Enter basketball id to delete")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
cursor.execute("DELETE FROM basketball WHERE _id = ?", (basketballId,))
if cursor.rowcount == 0:
print(f"{basketballId} does not exist")
else:
print(f"Successfully deleted basketball with id {basketballId}")
connection.commit()
except sqlite3.Error as error:
print("Error occurred: ", error)
# Closing cursor and connection
cursor.close()
connection.close()
delete()