diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/lab_sql_python_connection.ipynb b/lab_sql_python_connection.ipynb new file mode 100644 index 0000000..d684574 --- /dev/null +++ b/lab_sql_python_connection.ipynb @@ -0,0 +1,413 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "63bf87af", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection successful!\n" + ] + } + ], + "source": [ + "# Establishing a Connection to MySQL Database using SQLAlchemy\n", + "\n", + "from dotenv import load_dotenv\n", + "import os\n", + "from urllib.parse import quote_plus\n", + "\n", + "load_dotenv(override=True)\n", + "password = quote_plus(os.getenv(\"DB_PASSWORD\"))\n", + "\n", + "engine = create_engine(f\"mysql+pymysql://root:{password}@localhost/sakila\")\n", + "print(\"Connection successful!\")\n", + "\n", + "# Note:\n", + "# Special characters in the password (like `@`, `#`, `$`, etc.) can cause issues in the connection string if not properly encoded. By using `quote_plus`, we ensure that the password is safely included in the connection string without causing syntax errors.\n", + "# Character is then replaced by \"%21\" " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87c9c795", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating a Function to Retrieve Rentals for a Specific Month and Year\n", + "\n", + "def rentals_month(engine, month, year):\n", + " query = f\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " WHERE MONTH(rental_date) = {month}\n", + " AND YEAR(rental_date) = {year}\n", + " \"\"\"\n", + " return pd.read_sql(query, engine)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46e85046", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
012005-05-24 22:53:303671302005-05-26 22:04:3012006-02-15 21:30:53
122005-05-24 22:54:3315254592005-05-28 19:40:3312006-02-15 21:30:53
232005-05-24 23:03:3917114082005-06-01 22:12:3912006-02-15 21:30:53
342005-05-24 23:04:4124523332005-06-03 01:43:4122006-02-15 21:30:53
452005-05-24 23:05:2120792222005-06-02 04:33:2112006-02-15 21:30:53
\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1 2005-05-24 22:53:30 367 130 \n", + "1 2 2005-05-24 22:54:33 1525 459 \n", + "2 3 2005-05-24 23:03:39 1711 408 \n", + "3 4 2005-05-24 23:04:41 2452 333 \n", + "4 5 2005-05-24 23:05:21 2079 222 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-05-26 22:04:30 1 2006-02-15 21:30:53 \n", + "1 2005-05-28 19:40:33 1 2006-02-15 21:30:53 \n", + "2 2005-06-01 22:12:39 1 2006-02-15 21:30:53 \n", + "3 2005-06-03 01:43:41 2 2006-02-15 21:30:53 \n", + "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 " + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Testing the Function with May 2005 Rentals\n", + "df = rentals_month(engine, 5, 2005)\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f16316d", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating a Function to Count Rentals per Customer for a Specific Month and Year\n", + "\n", + "def rental_count_month(df, month, year):\n", + " count_df = df.groupby(\"customer_id\")[\"rental_id\"].count().reset_index()\n", + " count_df.columns = [\"customer_id\", f\"rentals_{month:02d}_{year}\"]\n", + " return count_df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c650bf80", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idrentals_05_2005
012
121
232
353
463
\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005\n", + "0 1 2\n", + "1 2 1\n", + "2 3 2\n", + "3 5 3\n", + "4 6 3" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Testing the Rental Count Function\n", + " \n", + "may_count = rental_count_month(df, 5, 2005)\n", + "may_count.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9469f93", + "metadata": {}, + "outputs": [], + "source": [ + "# Testing the Function with June 2005 Rentals\n", + "\n", + "df_june = rentals_month(engine, 6, 2005)\n", + "june_count = rental_count_month(df_june, 6, 2005)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84370109", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating a Function to Compare Rentals Between Two DataFrames\n", + "\n", + "def compare_rentals(df1, df2):\n", + " combined = pd.merge(df1, df2, on=\"customer_id\", how=\"outer\").fillna(0)\n", + " combined[\"difference\"] = combined.iloc[:, 1] - combined.iloc[:, 2]\n", + " return combined" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b5b67c7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idrentals_05_2005rentals_06_2005difference
012.07.0-5.0
121.01.00.0
232.04.0-2.0
340.06.0-6.0
453.05.0-2.0
\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_06_2005 difference\n", + "0 1 2.0 7.0 -5.0\n", + "1 2 1.0 1.0 0.0\n", + "2 3 2.0 4.0 -2.0\n", + "3 4 0.0 6.0 -6.0\n", + "4 5 3.0 5.0 -2.0" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Comparing May and June Rentals\n", + "\n", + "result = compare_rentals(may_count, june_count)\n", + "result.head()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab_sql_python_connection.sql b/lab_sql_python_connection.sql new file mode 100644 index 0000000..35b9622 --- /dev/null +++ b/lab_sql_python_connection.sql @@ -0,0 +1,17 @@ +-- Selecting database +USE sakila; + +-- Notes: +-- 1) Will be done in VS Code, stored in the same repo +-- 2) pip install sqlalchemy pymysql // To establish connection with SQL +-- 3) pip install python-dotenv // Installed to save password in .env file and not hard-code it in the notebook +-- 4) echo ".env" >> .gitignore // Ran in GitBash terminal so the .env file is not uploaded to the repo +-- 5) The following line of code needs to be executed if the SQL password contains special characters: + +-- from dotenv import load_dotenv +-- import os +-- from urllib.parse import quote_plus +-- load_dotenv(override=True) +-- password = quote_plus(os.getenv("DB_PASSWORD")) +-- engine = create_engine(f"mysql+pymysql://root:{password}@localhost/sakila") +-- print("Connection successful!") \ No newline at end of file