From bdaf17796192d945a3f2d619a73af898d8764789 Mon Sep 17 00:00:00 2001 From: Orgo4ever <163662002+Orgo4ever@users.noreply.github.com> Date: Fri, 10 Jul 2026 15:31:16 +0200 Subject: [PATCH] Create Lab-SQL-python-connection.ipynb Solved Lab --- Lab-SQL-python-connection.ipynb | 848 ++++++++++++++++++++++++++++++++ 1 file changed, 848 insertions(+) create mode 100644 Lab-SQL-python-connection.ipynb diff --git a/Lab-SQL-python-connection.ipynb b/Lab-SQL-python-connection.ipynb new file mode 100644 index 0000000..47b6529 --- /dev/null +++ b/Lab-SQL-python-connection.ipynb @@ -0,0 +1,848 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "094ff331", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: sqlalchemy in c:\\users\\kahau\\appdata\\local\\programs\\python\\python314\\lib\\site-packages (2.0.51)\n", + "Requirement already satisfied: greenlet>=1 in c:\\users\\kahau\\appdata\\local\\programs\\python\\python314\\lib\\site-packages (from sqlalchemy) (3.5.3)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in c:\\users\\kahau\\appdata\\roaming\\python\\python314\\site-packages (from sqlalchemy) (4.15.0)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "[notice] A new release of pip is available: 25.3 -> 26.1.2\n", + "[notice] To update, run: C:\\Users\\kahau\\AppData\\Local\\Programs\\Python\\Python314\\python.exe -m pip install --upgrade pip\n" + ] + } + ], + "source": [ + "!pip install --upgrade sqlalchemy" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40773330", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f23319f7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c:\\Program Files\\Python39\\python.exe\n" + ] + } + ], + "source": [ + "import sys\n", + "print(sys.executable)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3587eba2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import sys\n", + "import subprocess\n", + "\n", + "subprocess.check_call([\n", + " sys.executable,\n", + " \"-m\",\n", + " \"pip\",\n", + " \"install\",\n", + " \"--user\",\n", + " \"--upgrade\",\n", + " \"PyMySQL\"\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1788f2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.8\n" + ] + } + ], + "source": [ + "import pymysql\n", + "\n", + "print(pymysql.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ff4ad41d", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fcf86820", + "metadata": {}, + "outputs": [], + "source": [ + "import pymysql" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f9e0c2db", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d1ed3e36", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: greenlet==3.1.1 in c:\\users\\kahau\\appdata\\roaming\\python\\python39\\site-packages (3.1.1)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING: You are using pip version 21.2.4; however, version 26.0.1 is available.\n", + "You should consider upgrading via the 'c:\\Program Files\\Python39\\python.exe -m pip install --upgrade pip' command.\n" + ] + } + ], + "source": [ + "%pip install --only-binary=:all: greenlet==3.1.1" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "021c704f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: SQLAlchemy==2.0.44 in c:\\users\\kahau\\appdata\\roaming\\python\\python39\\site-packages (2.0.44)\n", + "Requirement already satisfied: greenlet>=1 in c:\\users\\kahau\\appdata\\roaming\\python\\python39\\site-packages (from SQLAlchemy==2.0.44) (3.1.1)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in c:\\users\\kahau\\appdata\\roaming\\python\\python39\\site-packages (from SQLAlchemy==2.0.44) (4.15.0)\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING: You are using pip version 21.2.4; however, version 26.0.1 is available.\n", + "You should consider upgrading via the 'c:\\Program Files\\Python39\\python.exe -m pip install --upgrade pip' command.\n" + ] + } + ], + "source": [ + "%pip install SQLAlchemy==2.0.44" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "2da7197d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0.44\n" + ] + } + ], + "source": [ + "import sqlalchemy\n", + "\n", + "print(sqlalchemy.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "5df970e0", + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import create_engine, text" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "4485a6fc", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import pymysql\n", + "import getpass\n", + "\n", + "from sqlalchemy import create_engine, text" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "05e03267", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "'grep' is not recognized as an internal or external command,\n", + "operable program or batch file.\n" + ] + } + ], + "source": [ + "pip show sqlalchemy | grep Version" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "08cd0d74", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Imports successful\n" + ] + } + ], + "source": [ + "import pymysql\n", + "from sqlalchemy import create_engine, text\n", + "\n", + "print(\"Imports successful\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "4a565ce0", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()\n", + "\n", + "# Note that when you use _SQLAlchemy_ and establish the connection, you do not even need to be logged in Sequel Pro or MySQL Workbench." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "a6839d75", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sa = \"sakila\"\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/'+sa\n", + "engine = create_engine(connection_string)\n", + "engine" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "7149ece8", + "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": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlalchemy import text\n", + "\n", + "with engine.connect() as connection:\n", + " query = text(\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " LIMIT 5;\n", + " \"\"\")\n", + "\n", + " result = connection.execute(query)\n", + " test_df = pd.DataFrame(result.fetchall(), columns=result.keys())\n", + "\n", + "test_df" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "af5ceb4d", + "metadata": {}, + "outputs": [], + "source": [ + "def rentals_month(engine, month, year):\n", + " query = text(\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " WHERE MONTH(rental_date) = :month\n", + " AND YEAR(rental_date) = :year\n", + " \"\"\")\n", + "\n", + " with engine.connect() as connection:\n", + " result = connection.execute(\n", + " query,\n", + " {\"month\": month, \"year\": year}\n", + " )\n", + "\n", + " rental_df = pd.DataFrame(\n", + " result.fetchall(),\n", + " columns=result.keys()\n", + " )\n", + "\n", + " return rental_df" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "6731c533", + "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": [ + "may_rentals = rentals_month(engine, 5, 2005)\n", + "may_rentals.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "35d9f8da", + "metadata": {}, + "outputs": [], + "source": [ + "def rental_count_month(rental_df, month, year):\n", + " rental_counts = (\n", + " rental_df\n", + " .groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=f\"rentals_{month:02d}_{year}\"))\n", + " return rental_counts \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "a38adb13", + "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": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_counts = rental_count_month(may_rentals, 5, 2005)\n", + "may_counts.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "97757321", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df1, df2):\n", + " comparison = pd.merge(\n", + " df1,\n", + " df2,\n", + " on=\"customer_id\",\n", + " how=\"inner\"\n", + " )\n", + " comparison[\"difference\"] = (\n", + " comparison.iloc[:, 2] - comparison.iloc[:, 1]\n", + " )\n", + "\n", + " return comparison" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "72819179", + "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
01275
12110
23242
35352
46341
\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_06_2005 difference\n", + "0 1 2 7 5\n", + "1 2 1 1 0\n", + "2 3 2 4 2\n", + "3 5 3 5 2\n", + "4 6 3 4 1" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "june_rentals = rentals_month(engine, 6, 2005)\n", + "june_counts = rental_count_month(june_rentals, 6, 2005)\n", + "\n", + "comparison = compare_rentals(may_counts, june_counts)\n", + "comparison.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27cc9eb1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.9.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}