{
 "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": "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",
    "    \n",
    "    "
   ]
  },
  {
   "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": "fc590047-1e58-4372-9da3-e99e1bd883d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "if conn.is_connected():\n",
    "       c2.close()\n",
    "       c1.close()\n",
    "       conn.close()\n",
    "       print('Done')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102b78c7-042c-443f-9ba3-e3ac95c943a5",
   "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
}