2022-12-10 22:59:07 +11:00

55 lines
1.9 KiB
Python

""" Tool to select random movies from our to watch database """
import sqlite3
#connection = sqlite3.connect("data.db")
def create_table():
""" Function to create new table if none already exists"""
with sqlite3.connect("data.db") as connection:
connection.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, genre TEXT, year INT)," \
"UNIQUE(title));")
# close_connection()
def add_movie(title, genre, year):
""" Function for adding movies to the db"""
with sqlite3.connect("data.db") as connection:
connection.execute("INSERT OR IGNORE INTO movies VALUES (?, ?, ?)", (title, genre, year))
# close_connection()
def select_random_movie():
""" Function for selecting random movie """
with sqlite3.connect("data.db") as connection:
rand_mov_list = connection.execute("SELECT * FROM movies ORDER BY RANDOM() LIMIT 1;")
for movie in rand_mov_list:
movie_name = (f"{movie[0]}, from {movie[2]}")
return movie_name
def main():
""" This is the main function of the script"""
print('Please enter your choice...')
main_prompt = ('''
****************************
1: Enter a movie
2: Get random movie
3: Exit
****************************
''')
while (user_input := input(main_prompt)) != "3":
if user_input == "1":
print("Adding movie")
movie_title = input("Enter Title: ")
movie_genre = input("Enter Genre: ")
movie_year = input("Enter Year: ")
add_movie(movie_title, movie_genre, movie_year)
print("Movie Added")
continue
if user_input == "2":
print("Selecting Movie...")
random_movie = select_random_movie()
print(f"Let's watch {random_movie}")
continue
print("Invalid option, please try again")
main()