{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68f82dc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pip install psycopg2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db00bbea",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pip install --user ipython-sql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "74ae19d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg2\n",
    "import sql"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29fdd335",
   "metadata": {},
   "source": [
    "## Verbinde zur Postgre Datenbank im Docker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "183d5c5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database connected successfully\n"
     ]
    }
   ],
   "source": [
    "conn = psycopg2.connect(\n",
    "    host=\"localhost\",\n",
    "    database=\"transparenz\",\n",
    "    user=\"postgres\",\n",
    "    password=\"postgres\")\n",
    "\n",
    "print(\"Database connected successfully\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e299175a",
   "metadata": {},
   "source": [
    "## Anlegen der Tabelle **Company**\n",
    "- Erstellen eines Cursors\n",
    "- für einen Primary key benötigt man den Typ \"Serial\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4ae6b45d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Table Created successfully\n"
     ]
    }
   ],
   "source": [
    "conn.autocommit = True\n",
    "#create a table\n",
    "cur = conn.cursor()  # creating a cursor\n",
    " \n",
    "# executing queries to create table\n",
    "cur.execute(\"\"\"\n",
    "CREATE TABLE company\n",
    "(\n",
    "    ID SERIAL PRIMARY KEY NOT NULL,\n",
    "    NAME TEXT NOT NULL,\n",
    "    STREET TEXT NOT NULL,\n",
    "    ZIP INT NOT NULL,\n",
    "    CITY VARCHAR(100) NOT NULL,\n",
    "    SECTOR VARCHAR(200) NOT NULL)\n",
    "\"\"\")\n",
    " \n",
    "# commit the changes\n",
    "conn.commit() # <--- makes sure the change is shown in the database\n",
    "#conn.close()\n",
    "#cur.close()\n",
    "print(\"Table Created successfully\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d59c0e9",
   "metadata": {},
   "source": [
    "## Anlegen der Tabelle **finance**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "32e46d88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Table Created successfully\n"
     ]
    }
   ],
   "source": [
    "conn.autocommit = True\n",
    "#create a table\n",
    "cur = conn.cursor()  # creating a cursor\n",
    " \n",
    "# executing queries to create table\n",
    "cur.execute(\"\"\"\n",
    "CREATE TABLE finance\n",
    "(\n",
    "    FINANCE_ID SERIAL PRIMARY KEY NOT NULL,\n",
    "    COMPANY_ID INT NOT NULL,\n",
    "    KIND_OF VARCHAR(50) NOT NULL,\n",
    "    DATE DATE NOT NULL,\n",
    "    SUM FLOAT NOT NULL)\n",
    "\"\"\")\n",
    " \n",
    "# commit the changes\n",
    "conn.commit() # <--- makes sure the change is shown in the database\n",
    "#conn.close()\n",
    "#cur.close()\n",
    "print(\"Table Created successfully\")"
   ]
  },
  {
   "attachments": {
    "grafik.png": {
     "image/png": "[encoded data, 118372 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "f910589e",
   "metadata": {},
   "source": [
    "Nach erfolgreichem Anlegen der Tabellen kann dies über pgAdmin kontrolliert werden.\n",
    "![grafik.png](attachment:grafik.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b96412a",
   "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
}
