{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "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": 32,
   "id": "5fcb85d1-36a2-41c1-8388-8049cfb8117c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: mysql-connector in /home/dekhtyar/.local/lib/python3.8/site-packages (2.2.9)\n"
     ]
    }
   ],
   "source": [
    "!pip install mysql-connector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "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": 34,
   "id": "07adf0b8-2936-47d7-add8-cf78a6511de4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connected to  mysql.labthreesixfive.com\n"
     ]
    }
   ],
   "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": 35,
   "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": 36,
   "id": "fd829318-c960-45ef-944b-bdcc3f2dcfb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Classes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Menus</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Restaurants</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ServesDishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ctmp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>tmp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>tst</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              0\n",
       "0       Classes\n",
       "1         Menus\n",
       "2   Restaurants\n",
       "3  ServesDishes\n",
       "4          ctmp\n",
       "5           tmp\n",
       "6           tst"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tbl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2fcd77b1-df23-4ac9-bf90-ab740158fe3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Table</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Classes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Menus</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Restaurants</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ServesDishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ctmp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>tmp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>tst</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Table\n",
       "0       Classes\n",
       "1         Menus\n",
       "2   Restaurants\n",
       "3  ServesDishes\n",
       "4          ctmp\n",
       "5           tmp\n",
       "6           tst"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 29,
   "id": "290b22bb-f4e8-478f-80b7-26e75334cc20",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'unread_result'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-29-d9918f74f835>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfetchall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/mysql/connector/cursor_cext.py\u001b[0m in \u001b[0;36mfetchall\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    608\u001b[0m         \"\"\"\n\u001b[1;32m    609\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_executed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 610\u001b[0;31m         \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_cnx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munread_result\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    611\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    612\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'unread_result'"
     ]
    }
   ],
   "source": [
    "c2.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5d53bb41-5b93-4078-8136-276892c4548b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'class': 'CSC 365', 'section': 7, 'startDate': None, 'startTime': None}, {'class': 'CSC 365', 'section': 8, 'startDate': datetime.date(2024, 4, 2), 'startTime': datetime.timedelta(seconds=43800)}]\n",
      "------ Classes----------\n",
      "\n",
      "{'class': 'CSC 365', 'section': 7, 'startDate': None, 'startTime': None}\n",
      "{'class': 'CSC 365', 'section': 8, 'startDate': datetime.date(2024, 4, 2), 'startTime': datetime.timedelta(seconds=43800)}\n",
      "----------------------\n",
      "\n",
      "\n",
      "[{'Id': 1, 'Name': 'Chicken Burrito', 'DishType': 'Main Dish'}, {'Id': 2, 'Name': 'Crunchwrap Supreme', 'DishType': 'Main Dish'}, {'Id': 3, 'Name': 'Gyros Sandwich', 'DishType': 'Main Dish'}, {'Id': 4, 'Name': 'Chicken Kebab', 'DishType': 'Main Dish'}, {'Id': 5, 'Name': 'Lentil Soup', 'DishType': 'Soup'}, {'Id': 6, 'Name': 'Hummus', 'DishType': 'Appetizer'}, {'Id': 7, 'Name': 'Diet Coke', 'DishType': 'Drink'}]\n",
      "------ Menus----------\n",
      "\n",
      "{'Id': 1, 'Name': 'Chicken Burrito', 'DishType': 'Main Dish'}\n",
      "{'Id': 2, 'Name': 'Crunchwrap Supreme', 'DishType': 'Main Dish'}\n",
      "{'Id': 3, 'Name': 'Gyros Sandwich', 'DishType': 'Main Dish'}\n",
      "{'Id': 4, 'Name': 'Chicken Kebab', 'DishType': 'Main Dish'}\n",
      "{'Id': 5, 'Name': 'Lentil Soup', 'DishType': 'Soup'}\n",
      "{'Id': 6, 'Name': 'Hummus', 'DishType': 'Appetizer'}\n",
      "{'Id': 7, 'Name': 'Diet Coke', 'DishType': 'Drink'}\n",
      "----------------------\n",
      "\n",
      "\n",
      "[{'Id': 1, 'Name': 'Petra', 'Address': 'Higuera St.', 'Cuisine': 'Mediterrenean', 'HealthScore': 92, 'Yelp': 4.5}, {'Id': 2, 'Name': 'Taco Bell', 'Address': 'Broad St.', 'Cuisine': 'Fast Mexican', 'HealthScore': 81, 'Yelp': 4.0}, {'Id': 3, 'Name': 'Taco Bell', 'Address': 'San Luis Obispo', 'Cuisine': 'Fast Mexican', 'HealthScore': 91, 'Yelp': 4.2}, {'Id': 4, 'Name': 'Hellahot', 'Address': 'San Luis Obispo', 'Cuisine': 'Tennessee Chicken', 'HealthScore': 91, 'Yelp': 4.2}, {'Id': 10, 'Name': 'Taco Bell', 'Address': 'Santa Rosa', 'Cuisine': 'Fast Mexican', 'HealthScore': 81, 'Yelp': 4.0}, {'Id': 11, 'Name': 'Chipotle', 'Address': 'San Luis Obispo', 'Cuisine': 'Mexican', 'HealthScore': 91, 'Yelp': 4.2}]\n",
      "------ Restaurants----------\n",
      "\n",
      "{'Id': 1, 'Name': 'Petra', 'Address': 'Higuera St.', 'Cuisine': 'Mediterrenean', 'HealthScore': 92, 'Yelp': 4.5}\n",
      "{'Id': 2, 'Name': 'Taco Bell', 'Address': 'Broad St.', 'Cuisine': 'Fast Mexican', 'HealthScore': 81, 'Yelp': 4.0}\n",
      "{'Id': 3, 'Name': 'Taco Bell', 'Address': 'San Luis Obispo', 'Cuisine': 'Fast Mexican', 'HealthScore': 91, 'Yelp': 4.2}\n",
      "{'Id': 4, 'Name': 'Hellahot', 'Address': 'San Luis Obispo', 'Cuisine': 'Tennessee Chicken', 'HealthScore': 91, 'Yelp': 4.2}\n",
      "{'Id': 10, 'Name': 'Taco Bell', 'Address': 'Santa Rosa', 'Cuisine': 'Fast Mexican', 'HealthScore': 81, 'Yelp': 4.0}\n",
      "{'Id': 11, 'Name': 'Chipotle', 'Address': 'San Luis Obispo', 'Cuisine': 'Mexican', 'HealthScore': 91, 'Yelp': 4.2}\n",
      "----------------------\n",
      "\n",
      "\n",
      "[{'RestaurantId': 2, 'DishId': 1, 'Price': Decimal('4.99')}, {'RestaurantId': 2, 'DishId': 2, 'Price': Decimal('5.49')}, {'RestaurantId': 3, 'DishId': 1, 'Price': Decimal('4.99')}, {'RestaurantId': 3, 'DishId': 3, 'Price': Decimal('6.99')}]\n",
      "------ ServesDishes----------\n",
      "\n",
      "{'RestaurantId': 2, 'DishId': 1, 'Price': Decimal('4.99')}\n",
      "{'RestaurantId': 2, 'DishId': 2, 'Price': Decimal('5.49')}\n",
      "{'RestaurantId': 3, 'DishId': 1, 'Price': Decimal('4.99')}\n",
      "{'RestaurantId': 3, 'DishId': 3, 'Price': Decimal('6.99')}\n",
      "----------------------\n",
      "\n",
      "\n",
      "[]\n",
      "------ ctmp----------\n",
      "\n",
      "----------------------\n",
      "\n",
      "\n",
      "[{'id': 1, 'val': 'Bob', 'f': 5}, {'id': 2, 'val': 'Alice', 'f': 5}, {'id': 1, 'val': 'Chuck', 'f': 5}, {'id': 1, 'val': 'This is a long string', 'f': 6}, {'id': 10, 'val': 'WOW', 'f': 5}]\n",
      "------ tmp----------\n",
      "\n",
      "{'id': 1, 'val': 'Bob', 'f': 5}\n",
      "{'id': 2, 'val': 'Alice', 'f': 5}\n",
      "{'id': 1, 'val': 'Chuck', 'f': 5}\n",
      "{'id': 1, 'val': 'This is a long string', 'f': 6}\n",
      "{'id': 10, 'val': 'WOW', 'f': 5}\n",
      "----------------------\n",
      "\n",
      "\n",
      "[]\n",
      "------ tst----------\n",
      "\n",
      "----------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for t in tbl['Table']:\n",
    "    c2 = conn.cursor(dictionary = True)\n",
    "    c2.execute('SELECT * FROM '+ t)\n",
    "    records = c2.fetchall()\n",
    "    print(records)\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": 11,
   "id": "579da83b-549a-4e7e-bcf1-dfee4234719d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "records"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3d7a6030-7d41-451a-9a3f-e5cab2949a64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'Id': 3, 'Name': 'Gyros Sandwich', 'DishType': 'Main Dish'},\n",
       " {'Id': 4, 'Name': 'Chicken Kebab', 'DishType': 'Main Dish'},\n",
       " {'Id': 5, 'Name': 'Lentil Soup', 'DishType': 'Soup'},\n",
       " {'Id': 6, 'Name': 'Hummus', 'DishType': 'Appetizer'},\n",
       " {'Id': 7, 'Name': 'Diet Coke', 'DishType': 'Drink'}]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c3.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4941dd79-b90b-4c09-bd69-03d42259cc96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Id': 1, 'Name': 'Chicken Burrito', 'DishType': 'Main Dish'}\n",
      "{'Id': 2, 'Name': 'Crunchwrap Supreme', 'DishType': 'Main Dish'}\n",
      "Chicken Burrito\n"
     ]
    }
   ],
   "source": [
    "## Let's use fetch_one()\n",
    "\n",
    "c3 = conn.cursor(dictionary=True)\n",
    "\n",
    "query = 'SELECT * FROM Menus'\n",
    "\n",
    "c3.execute(query)\n",
    "\n",
    "\n",
    "\n",
    "rec1 = c3.fetchone()\n",
    "rec2 = c3.fetchone()\n",
    "#rec3 = c3.fetchone()\n",
    "#rec4 = c3.fetchone()\n",
    "\n",
    "print(rec1)\n",
    "print(rec2)\n",
    "\n",
    "print(rec1[\"Name\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d651d9c5-69d4-46d2-a525-1dde78c37dad",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Let's use fetch_one()\n",
    "\n",
    "c11 = conn.cursor(dictionary=True)\n",
    "\n",
    "query = 'SELECT * FROM Menus'\n",
    "\n",
    "c11.execute(query)\n",
    "\n",
    "data = pd.DataFrame(c11)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2a370f8a-7c72-49cd-888f-5f2c6b71fc42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>Name</th>\n",
       "      <th>DishType</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Chicken Burrito</td>\n",
       "      <td>Main Dish</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Crunchwrap Supreme</td>\n",
       "      <td>Main Dish</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Gyros Sandwich</td>\n",
       "      <td>Main Dish</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Chicken Kebab</td>\n",
       "      <td>Main Dish</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Lentil Soup</td>\n",
       "      <td>Soup</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Hummus</td>\n",
       "      <td>Appetizer</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Diet Coke</td>\n",
       "      <td>Drink</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Id                Name   DishType\n",
       "0   1     Chicken Burrito  Main Dish\n",
       "1   2  Crunchwrap Supreme  Main Dish\n",
       "2   3      Gyros Sandwich  Main Dish\n",
       "3   4       Chicken Kebab  Main Dish\n",
       "4   5         Lentil Soup       Soup\n",
       "5   6              Hummus  Appetizer\n",
       "6   7           Diet Coke      Drink"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f2303d72-54b9-4cd8-9b86-ae74a6de82ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(records)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "905e004b-e4dc-4152-a161-1bbb60f800c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#### FIX THIS IN CLASS INSERT INTO RESTAURANTS OR MENUS\n",
    "\n",
    "c4 = conn.cursor()\n",
    "# (3, 'Chemistry', 'and Biochemistry', 'COSAM', 180, '206')\n",
    "\n",
    "insert = \"INSERT INTO Departments VALUES(4, 'Physics', 'Physics', 'COSAM', 180, '204')\"\n",
    "\n",
    "c4.execute(insert)\n",
    "\n",
    "c4.execute(\"SELECT * FROM Departments\")\n",
    "\n",
    "r = pd.DataFrame(c4)\n",
    "\n",
    "r\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9589905b-8004-4cdd-8a00-36da2f9e2eee",
   "metadata": {},
   "outputs": [],
   "source": [
    "c5 = conn.cursor()\n",
    "\n",
    "stm  = \"DELETE FROM st where val = '''ab'''\"\n",
    "\n",
    "stm2 = 'SELECT * FROM st'\n",
    "\n",
    "c5.execute(stm)\n",
    "#c5.execute(stm2)\n",
    "#d = pd.DataFrame(c5)\n",
    "\n",
    "\n",
    "#d\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": "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
}