import sqlite3

# Connect to the database
conn = sqlite3.connect('movies.db')

# Create a cursor object to execute SQL commands
c = conn.cursor()

# Create the table if it doesn't exist
c.execute('''
          CREATE TABLE IF NOT EXISTS movies (
              id INTEGER PRIMARY KEY AUTOINCREMENT,
              title TEXT NOT NULL,
              director TEXT NOT NULL,
              year INTEGER NOT NULL
          )
          ''')

# Define a function to add a new movie to the table
def add_movie(title, director, year):
    c.execute("INSERT INTO movies (title, director, year) VALUES (?, ?, ?)", (title, director, year))
    conn.commit()

# Define a function to update an existing movie in the table
def update_movie(movie_id, title=None, director=None, year=None):
    # Create a list of tuples containing the column name and new value for each provided parameter
    updates = [(col, val) for col, val in [("title", title), ("director", director), ("year", year)] if val is not None]
    
    # Construct an SQL UPDATE query string with placeholders for the new values
    update_query = ", ".join([f"{col} = ?" for col, _ in updates])
    
    # Create a list of values to replace the placeholders in the query string
    update_params = [val for _, val in updates] + [movie_id]
    
    # Execute the UPDATE query with the new values and commit the changes
    c.execute(f"UPDATE movies SET {update_query} WHERE id = ?", update_params)
    conn.commit()

# Define a function to delete a movie from the table
def delete_movie(movie_id):
    c.execute("DELETE FROM movies WHERE id = ?", (movie_id,))
    conn.commit()

# Define a function to retrieve a list of all movies in the table
def get_all_movies():
    c.execute("SELECT * FROM movies")
    return c.fetchall()

# Example usage of the functions
add_movie("Inception", "Christopher Nolan", 2010)
add_movie("The Godfather", "Francis Ford Coppola", 1972)
add_movie("The Shawshank Redemption", "Frank Darabont", 1994)
add_movie("Forrest Gump", "Robert Zemeckis", 1994)
add_movie("Invictus", "Clint Eastwood", 2009)
add_movie("The Sting", "George Roy Hill", 1974)
add_movie("Far Away", "Ron Howard", 1992)

# Update the title and director of the movie with id 2
update_movie(2, title="The Godfather: Part II", director="Francis Ford Coppola")

# Delete the movie with id 4
delete_movie(4)

delete_movie(111)

# Print all movies in the table
movies = get_all_movies()
for movie in movies:
    print(movie)
(1, 'Inception', 'Christopher Nolan', 2010)
(2, 'The Godfather: Part II', 'Francis Ford Coppola', 1972)
(3, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(5, 'Invictus', 'Clint Eastwood', 2009)
(6, 'The Sting', 'George Roy Hill', 1974)
(8, 'The Godfather', 'Francis Ford Coppola', 1972)
(9, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(10, 'Forrest Gump', 'Robert Zemeckis', 1994)
(11, 'Invictus', 'Clint Eastwood', 2009)
(12, 'The Sting', 'George Roy Hill', 1974)
(13, 'Inception', 'Christopher Nolan', 2010)
(14, 'The Godfather', 'Francis Ford Coppola', 1972)
(15, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(16, 'Forrest Gump', 'Robert Zemeckis', 1994)
(17, 'Invictus', 'Clint Eastwood', 2009)
(18, 'The Sting', 'George Roy Hill', 1974)
(19, 'Inception', 'Christopher Nolan', 2010)
(20, 'The Godfather', 'Francis Ford Coppola', 1972)
(21, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(22, 'Forrest Gump', 'Robert Zemeckis', 1994)
(23, 'Invictus', 'Clint Eastwood', 2009)
(24, 'The Sting', 'George Roy Hill', 1974)
(25, 'Inception', 'Christopher Nolan', 2010)
(26, 'The Godfather', 'Francis Ford Coppola', 1972)
(27, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(28, 'Forrest Gump', 'Robert Zemeckis', 1994)
(29, 'Invictus', 'Clint Eastwood', 2009)
(30, 'The Sting', 'George Roy Hill', 1974)
(31, 'Inception', 'Christopher Nolan', 2010)
(32, 'The Godfather', 'Francis Ford Coppola', 1972)
(33, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(34, 'Forrest Gump', 'Robert Zemeckis', 1994)
(35, 'Invictus', 'Clint Eastwood', 2009)
(36, 'The Sting', 'George Roy Hill', 1974)
(37, 'Inception', 'Christopher Nolan', 2010)
(38, 'The Godfather', 'Francis Ford Coppola', 1972)
(39, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(40, 'Forrest Gump', 'Robert Zemeckis', 1994)
(41, 'Invictus', 'Clint Eastwood', 2009)
(42, 'The Sting', 'George Roy Hill', 1974)
(43, 'Inception', 'Christopher Nolan', 2010)
(44, 'The Godfather', 'Francis Ford Coppola', 1972)
(45, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(46, 'Forrest Gump', 'Robert Zemeckis', 1994)
(47, 'Invictus', 'Clint Eastwood', 2009)
(48, 'The Sting', 'George Roy Hill', 1974)
(49, 'Inception', 'Christopher Nolan', 2010)
(50, 'The Godfather', 'Francis Ford Coppola', 1972)
(51, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(52, 'Forrest Gump', 'Robert Zemeckis', 1994)
(53, 'Invictus', 'Clint Eastwood', 2009)
(54, 'The Sting', 'George Roy Hill', 1974)
(55, 'Inception', 'Christopher Nolan', 2010)
(56, 'The Godfather', 'Francis Ford Coppola', 1972)
(57, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(58, 'Forrest Gump', 'Robert Zemeckis', 1994)
(59, 'Invictus', 'Clint Eastwood', 2009)
(60, 'The Sting', 'George Roy Hill', 1974)
(61, 'Inception', 'Christopher Nolan', 2010)
(62, 'The Godfather', 'Francis Ford Coppola', 1972)
(63, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(64, 'Forrest Gump', 'Robert Zemeckis', 1994)
(65, 'Invictus', 'Clint Eastwood', 2009)
(66, 'The Sting', 'George Roy Hill', 1974)
(67, 'Inception', 'Christopher Nolan', 2010)
(68, 'The Godfather', 'Francis Ford Coppola', 1972)
(69, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(70, 'Forrest Gump', 'Robert Zemeckis', 1994)
(71, 'Invictus', 'Clint Eastwood', 2009)
(72, 'The Sting', 'George Roy Hill', 1974)
(73, 'Inception', 'Christopher Nolan', 2010)
(74, 'The Godfather', 'Francis Ford Coppola', 1972)
(75, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(76, 'Forrest Gump', 'Robert Zemeckis', 1994)
(77, 'Invictus', 'Clint Eastwood', 2009)
(78, 'The Sting', 'George Roy Hill', 1974)
(79, 'Inception', 'Christopher Nolan', 2010)
(80, 'The Godfather', 'Francis Ford Coppola', 1972)
(81, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(82, 'Forrest Gump', 'Robert Zemeckis', 1994)
(83, 'Invictus', 'Clint Eastwood', 2009)
(84, 'The Sting', 'George Roy Hill', 1974)
(85, 'Inception', 'Christopher Nolan', 2010)
(86, 'The Godfather', 'Francis Ford Coppola', 1972)
(87, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(88, 'Forrest Gump', 'Robert Zemeckis', 1994)
(89, 'Invictus', 'Clint Eastwood', 2009)
(90, 'The Sting', 'George Roy Hill', 1974)
(91, 'Inception', 'Christopher Nolan', 2010)
(92, 'The Godfather', 'Francis Ford Coppola', 1972)
(93, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(94, 'Forrest Gump', 'Robert Zemeckis', 1994)
(95, 'Invictus', 'Clint Eastwood', 2009)
(96, 'The Sting', 'George Roy Hill', 1974)
(97, 'Far Away', 'Ron Howard', 1992)
(98, 'Inception', 'Christopher Nolan', 2010)
(99, 'The Godfather', 'Francis Ford Coppola', 1972)
(100, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(101, 'Forrest Gump', 'Robert Zemeckis', 1994)
(102, 'Invictus', 'Clint Eastwood', 2009)
(103, 'The Sting', 'George Roy Hill', 1974)
(105, 'Inception', 'Christopher Nolan', 2010)
(106, 'The Godfather', 'Francis Ford Coppola', 1972)
(107, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(108, 'Forrest Gump', 'Robert Zemeckis', 1994)
(109, 'Invictus', 'Clint Eastwood', 2009)
(110, 'The Sting', 'George Roy Hill', 1974)
(112, 'Inception', 'Christopher Nolan', 2010)
(113, 'The Godfather', 'Francis Ford Coppola', 1972)
(114, 'The Shawshank Redemption', 'Frank Darabont', 1994)
(115, 'Forrest Gump', 'Robert Zemeckis', 1994)
(116, 'Invictus', 'Clint Eastwood', 2009)
(117, 'The Sting', 'George Roy Hill', 1974)
(118, 'Far Away', 'Ron Howard', 1992)