{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc9bf87d-5b60-4dac-a52c-d2ab8de6de2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mysql.connector\n",
    "from mysql.connector import Error\n",
    "\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "342b00b9-ad17-4d2f-aba5-afece48e1aa1",
   "metadata": {},
   "source": [
    "Short demo of the work of Python MySQL connectivity in the context of a Jupyter Labs Notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fcb85d1-36a2-41c1-8388-8049cfb8117c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install mysql-connector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6b5bacd-c5b4-4ff7-9129-2fa7a82c3689",
   "metadata": {},
   "outputs": [],
   "source": [
    "# setup\n",
    "\n",
    "pwdfileName = 'pss'               ## I am \"hiding\" my password in the pss file\n",
    "pwdfile = open(pwdfileName,'r')   ## opening the password file for reading\n",
    "p = pwdfile.read()[:-1]           ## extracting the password\n",
    "\n",
    "\n",
    "hostName = 'mysql.labthreesixfive.com'\n",
    "portName = '3306'\n",
    "userName = 'dekhtyar'\n",
    "passString =  p\n",
    "\n",
    "dbName = 'DEMOS'\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07adf0b8-2936-47d7-add8-cf78a6511de4",
   "metadata": {},
   "outputs": [],
   "source": [
    "### set up MySQL connection\n",
    "\n",
    "try:\n",
    " \n",
    "   conn = mysql.connector.connect(host = hostName, port = portName, database = dbName,\n",
    "                                        user = userName, password = passString)\n",
    "   if conn.is_connected():\n",
    "       print('Connected to ',hostName)\n",
    "       p=''\n",
    "except Error as e:\n",
    "    print('Connection Error:', e)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f03fc79c-844b-494d-8908-e2f0c524a1b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's see what's in the DEMOS database\n",
    "\n",
    "c1 = conn.cursor()\n",
    "\n",
    "c1.execute('show tables')\n",
    "tbl = pd.DataFrame(c1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fcd77b1-df23-4ac9-bf90-ab740158fe3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "tbl.columns=['Table']\n",
    "\n",
    "tbl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e28be021-177b-4485-a7e0-8b6f46314dbf",
   "metadata": {},
   "source": [
    "Let's print the contents of each table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d53bb41-5b93-4078-8136-276892c4548b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in tbl['Table']:\n",
    "    c2 = conn.cursor()\n",
    "    c2.execute('SELECT * FROM '+ t)\n",
    "    records = c2.fetchall()\n",
    "    print('------ '+t+'----------\\n')\n",
    "    for r in records:\n",
    "        print(r)\n",
    "    print('----------------------\\n\\n')\n",
    "    c2.close()\n",
    "    \n",
    "   # print(records)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4941dd79-b90b-4c09-bd69-03d42259cc96",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Let's use fetch_one()\n",
    "\n",
    "c3 = conn.cursor()\n",
    "\n",
    "print(\"Got here\")\n",
    "\n",
    "query = 'SELECT * FROM Classes'\n",
    "\n",
    "c3.execute(query)\n",
    "\n",
    "rec1 = c3.fetchone()\n",
    "#rec2 = c3.fetchone()\n",
    "#rec3 = c3.fetchone()\n",
    "#rec4 = c3.fetchone()\n",
    "\n",
    "print(rec1)\n",
    "\n",
    "t = c3.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2303d72-54b9-4cd8-9b86-ae74a6de82ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(records)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2ca4dcd-0a40-43df-9ac5-a2c40df7e3cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b6244a23-3024-4bf3-b6ac-17e2b268eb70",
   "metadata": {},
   "source": [
    "run the cell below at the very end of your session to close the connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cf1423f-5262-4efb-be39-1b80566c969a",
   "metadata": {},
   "outputs": [],
   "source": [
    "c5.execute(\"commit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc590047-1e58-4372-9da3-e99e1bd883d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "if conn.is_connected():\n",
    "       c2.close()\n",
    "       c1.close()\n",
    "       c3.close()\n",
    "       conn.close()\n",
    "       print('Done')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102b78c7-042c-443f-9ba3-e3ac95c943a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "370992eb-3f0d-4433-87d4-636acc26667f",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "try:\n",
    " \n",
    "   conn = mysql.connector.connect(host = hostName, port = portName, database = dbName,\n",
    "                                        user = userName, password = passString)\n",
    "   if conn.is_connected():\n",
    "       print('Connected to ',hostName)\n",
    "       p=''\n",
    "    \n",
    "   c5 = conn.cursor()\n",
    "\n",
    "   stm  = \"DELETE FROM st where val = '''ab'''\"\n",
    "\n",
    "   stm2 = 'SELECT * FROM st'\n",
    "   c5.execute(stm)\n",
    "   c5.execute(stm2)\n",
    "   print(\"Query\")\n",
    "   d = pd.DataFrame(c5)\n",
    "   print(d)\n",
    "    \n",
    "except Error as e:\n",
    "    print('Connection Error:', e)\n",
    "    \n",
    "finally:\n",
    "    c5.close()\n",
    "    conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f4094fe-6bfa-47f6-a131-f29f36851ed6",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}