Welcome to the Movie Database tutorial, where we show you how to create a Python program to manage and store movie data efficiently.
Introduction
In this tutorial, we will create a simple movie database application using Python. The program will allow you to store, retrieve, and manage information about movies, such as title, genre, release year, and ratings. This can be an excellent exercise for understanding how to work with databases, data storage, and basic CRUD (Create, Read, Update, Delete) operations in Python.
Objective
The objective of this program is to build a movie database system where users can add new movies, view existing movies, update details, and delete movies as needed. We will use SQLite, a lightweight database engine that comes with Python, for storing the movie information in a database.
Python Code for Movie Database
# Importing necessary libraries import sqlite3 # Function to connect to the database def connect_db(): conn = sqlite3.connect('movies.db') return conn # Function to create the movie table def create_table(): conn = connect_db() cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS movies ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, genre TEXT, release_year INTEGER, rating REAL ) ''') conn.commit() conn.close() # Function to add a new movie to the database def add_movie(title, genre, release_year, rating): conn = connect_db() cursor = conn.cursor() cursor.execute(''' INSERT INTO movies (title, genre, release_year, rating) VALUES (?, ?, ?, ?) ''', (title, genre, release_year, rating)) conn.commit() conn.close() # Function to fetch all movies from the database def view_movies(): conn = connect_db() cursor = conn.cursor() cursor.execute('SELECT * FROM movies') rows = cursor.fetchall() conn.close() return rows # Function to update a movie's rating def update_movie(id, new_rating): conn = connect_db() cursor = conn.cursor() cursor.execute(''' UPDATE movies SET rating = ? WHERE id = ? ''', (new_rating, id)) conn.commit() conn.close() # Function to delete a movie from the database def delete_movie(id): conn = connect_db() cursor = conn.cursor() cursor.execute('DELETE FROM movies WHERE id = ?', (id,)) conn.commit() conn.close() # Main function to interact with the database def main(): create_table() # Create table if not exists # Adding some sample movies add_movie('Inception', 'Sci-Fi', 2010, 8.8) add_movie('The Dark Knight', 'Action', 2008, 9.0) add_movie('Forrest Gump', 'Drama', 1994, 8.8) # Viewing all movies movies = view_movies() print("All Movies in Database:") for movie in movies: print(movie) # Updating a movie rating update_movie(1, 9.0) # Update rating of movie with id 1 # Deleting a movie delete_movie(2) # Delete movie with id 2 # Final list of movies movies = view_movies() print("\nFinal Movies List after update and delete:") for movie in movies: print(movie) if __name__ == '__main__': main()
Program Explanation
This Python program uses SQLite to manage a movie database. The program consists of several functions:
- connect_db: Establishes a connection to the SQLite database.
- create_table: Creates the ‘movies’ table in the database if it doesn’t exist.
- add_movie: Inserts a new movie record into the database with title, genre, release year, and rating.
- view_movies: Fetches and displays all the movies currently stored in the database.
- update_movie: Updates the rating of a movie based on its ID.
- delete_movie: Deletes a movie record from the database by its ID.
- main: The main function that runs the program. It creates the table, adds sample movies, displays the movies, updates a movie’s rating, and deletes a movie.
How to Run the Program
- Ensure you have Python installed on your system.
- Copy the code into a Python file, e.g.,
movie_database.py
. - Run the program using the command
python movie_database.py
from your terminal or command prompt. - The program will create a
movies.db
SQLite database file in the same directory and perform the operations such as adding, updating, and deleting movies.