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/movies.db'
createTblSql = """ CREATE TABLE IF NOT EXISTS movies (
_id integer PRIMARY KEY,
_title text NOT NULL,
_company text NOT NULL,
_character text NOT NULL,
_rottentomatoes text NOT NULL
); """
connection = create_connection(database)
# create animated movies table
if connection is not None:
create_table(connection, createTblSql)
else:
print('Connection Error')
if __name__ == '__main__':
main()
def create():
database = 'instance/movies.db'
title = input("Enter the movie title")
company = input("Enter movie company name")
character = input("Enter main character name")
rottentomatoes = input("Enter rottentomatoes of movie")
# 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 movies (_title, _company, _character, _rottentomatoes) VALUES (?, ?, ?, ?)", (title, company, character, rottentomatoes))
# Commit the changes
connection.commit()
print(f"New movie with {title} 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/movies.db'
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
# Fetch all the records from movies table
results = cursor.execute('SELECT * FROM movies').fetchall()
if len(results) != 0:
for row in results:
print(row)
else:
print("No movies")
# Closing cursor and connection
cursor.close()
connection.close()
read()
import sqlite3
# updating rottentomatoes
def update():
database = 'instance/movies.db'
movieId = input("Enter a movie id to update the rottentomatoes")
rottentomatoes = input("Enter new rottentomatoes")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
# Updating rottentomatoes for the movie
cursor.execute("UPDATE movies SET _rottentomatoes = ? WHERE _id = ?", (rottentomatoes, movieId))
if cursor.rowcount != 0:
print(f"rottentomatoes for the movie is updated to {rottentomatoes}")
connection.commit()
else:
print(f"movie 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/movies.db'
movieId = input("Enter movie 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 movies WHERE _id = ?", (movieId,))
if cursor.rowcount == 0:
print(f"{movieId} does not exist")
else:
print(f"Successfully deleted movie with id {movieId}")
connection.commit()
except sqlite3.Error as error:
print("Error occurred: ", error)
# Closing cursor and connection
cursor.close()
connection.close()
delete()
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")