Zoo Task finished
This commit is contained in:
1 parent
29884fd36d
commit
c8d8754282
3 files changed
+171
-68
No files matched your search
@@ -13,36 +13,34 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import psycopg2\n",
|
||||
"import psycopg2.extras"
|
||||
"import psycopg2.extras\n",
|
||||
"import os\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Verbindung aufbauen\n",
|
||||
"# TODO: hier müssen Ihre Verbindungsdaten eingetragen werden\n",
|
||||
"# TODO: ggf. müssen Sie auch den Namen der Datenbank anpassen oder eine leere Datenbank 'zoo' anlegen\n",
|
||||
"\n",
|
||||
"conn = psycopg2.connect(\n",
|
||||
" host=\"localhost\",\n",
|
||||
" port=5432,\n",
|
||||
" dbname=\"postgres\",\n",
|
||||
" user=\"postgres\",\n",
|
||||
" password=\"postgres\",\n",
|
||||
" host=os.getenv('PGHOST', 'localhost'),\n",
|
||||
" port=int(os.getenv('PGPORT', 5432)),\n",
|
||||
" dbname=os.getenv('PGDATABASE', 'zoo'),\n",
|
||||
" user=os.getenv('PGUSER', 'postgres'),\n",
|
||||
" password=os.getenv('PGPASSWORD', 'postgres'),\n",
|
||||
")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -75,66 +73,74 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# TODO: Implementieren Sie hier die CRUD-Operationen für Zookeeper\n",
|
||||
"\n",
|
||||
"# TODO: Tierpleger anlegen, liefert die ID des neuen Tierpflegers zurück\n",
|
||||
"# CRUD-Operationen für Zookeeper\n",
|
||||
"def create_zookeeper(name, email, specialty):\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cur:\n",
|
||||
" cur.execute(\"INSERT INTO zookeepers (name, email, specialty) VALUES (%s, %s, %s) RETURNING id\", (name, email, specialty))\n",
|
||||
" new_id = cur.fetchone()[0]\n",
|
||||
" conn.commit()\n",
|
||||
" return new_id\n",
|
||||
"\n",
|
||||
"# TODO: Tierpfleger nach ID lesen, Tupel zurückgeben\n",
|
||||
"def read_zookeeper(id):\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cur:\n",
|
||||
" cur.execute(\"SELECT id, name, email, specialty FROM zookeepers WHERE id = %s\", (id,))\n",
|
||||
" row = cur.fetchone()\n",
|
||||
" return row\n",
|
||||
"\n",
|
||||
"# TODO: Alle Tierpfleger lesen, liefert eine Liste von Tupeln zurück (nach ID sortiert)\n",
|
||||
"def read_all_zookeepers():\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cur:\n",
|
||||
" cur.execute(\"SELECT id, name, email, specialty FROM zookeepers ORDER BY id\")\n",
|
||||
" rows = cur.fetchall()\n",
|
||||
" return rows\n",
|
||||
"\n",
|
||||
"# TODO: Tierpfleger aktualisieren\n",
|
||||
"def update_zookeeper(id, name, email, specialty):\n",
|
||||
" pass\n",
|
||||
" with conn.cursor() as cur:\n",
|
||||
" cur.execute(\"UPDATE zookeepers SET name = %s, email = %s, specialty = %s WHERE id = %s\", (name, email, specialty, id))\n",
|
||||
" conn.commit()\n",
|
||||
"\n",
|
||||
"# TODO: Tierpfleger per ID löschen\n",
|
||||
"def delete_zookeeper(id):\n",
|
||||
" pass"
|
||||
" with conn.cursor() as cur:\n",
|
||||
" cur.execute(\"DELETE FROM zookeepers WHERE id = %s\", (id,))\n",
|
||||
" conn.commit()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tests - diese sollten alle erfolgreich durchlaufen werden und nicht verändert werden\n",
|
||||
"\n",
|
||||
"# john = (\"John Doe\", \"john@example.com\", \"Elephants\")\n",
|
||||
"# jane = (\"Jane Doe\", \"jane@example.com\", \"Giraffes\")\n",
|
||||
"john = (\"John Doe\", \"john@example.com\", \"Elephants\")\n",
|
||||
"jane = (\"Jane Doe\", \"jane@example.com\", \"Giraffes\")\n",
|
||||
"\n",
|
||||
"# id = create_zookeeper(*john)\n",
|
||||
"# id2 = create_zookeeper(*jane)\n",
|
||||
"id = create_zookeeper(*john)\n",
|
||||
"id2 = create_zookeeper(*jane)\n",
|
||||
"\n",
|
||||
"# assert read_zookeeper(id) == (id, *john)\n",
|
||||
"# assert read_zookeeper(id2) == (id2, *jane)\n",
|
||||
"assert read_zookeeper(id) == (id, *john)\n",
|
||||
"assert read_zookeeper(id2) == (id2, *jane)\n",
|
||||
"\n",
|
||||
"# john = (\"John Smith\", \"john2@example.com\", \"Zebras\")\n",
|
||||
"# update_zookeeper(id, *john)\n",
|
||||
"# assert read_zookeeper(id) == (id, *john)\n",
|
||||
"john = (\"John Smith\", \"john2@example.com\", \"Zebras\")\n",
|
||||
"update_zookeeper(id, *john)\n",
|
||||
"assert read_zookeeper(id) == (id, *john)\n",
|
||||
"\n",
|
||||
"# all_zookeepers = read_all_zookeepers()\n",
|
||||
"# assert len(all_zookeepers) == 2\n",
|
||||
"# assert all_zookeepers[0] == (id, *john)\n",
|
||||
"# assert all_zookeepers[1] == (id2, *jane)\n",
|
||||
"all_zookeepers = read_all_zookeepers()\n",
|
||||
"assert len(all_zookeepers) == 2\n",
|
||||
"assert all_zookeepers[0] == (id, *john)\n",
|
||||
"assert all_zookeepers[1] == (id2, *jane)\n",
|
||||
"\n",
|
||||
"# delete_zookeeper(id)\n",
|
||||
"# delete_zookeeper(id2)\n",
|
||||
"# assert read_zookeeper(id) == None\n",
|
||||
"# assert read_zookeeper(id2) == None\n",
|
||||
"delete_zookeeper(id)\n",
|
||||
"delete_zookeeper(id2)\n",
|
||||
"assert read_zookeeper(id) == None\n",
|
||||
"assert read_zookeeper(id2) == None\n",
|
||||
"\n",
|
||||
"# all_zookeepers = read_all_zookeepers()\n",
|
||||
"# assert len(all_zookeepers) == 0"
|
||||
"all_zookeepers = read_all_zookeepers()\n",
|
||||
"assert len(all_zookeepers) == 0"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -152,11 +158,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"## Nutzung von SQLAlchemy\n",
|
||||
"import os\n",
|
||||
"from sqlalchemy import create_engine, Column, Integer, String, ForeignKey\n",
|
||||
"from sqlalchemy.orm import Session, relationship, declarative_base\n",
|
||||
"\n",
|
||||
@@ -178,8 +185,8 @@
|
||||
" zookeeper_id = Column(Integer, ForeignKey('zookeepers.id'))\n",
|
||||
" zookeeper = relationship(\"Zookeeper\", back_populates=\"animals\")\n",
|
||||
"\n",
|
||||
"# TODO: Verbingung zur Datenbank herstellen\n",
|
||||
"engine = create_engine('postgresql://IHR_POSTGRES_USER:IHR_POSTGRES_PASSWORD/zoo')"
|
||||
"# Verbindung zur Datenbank herstellen\n",
|
||||
"engine = create_engine(os.getenv('DATABASE_URL', 'postgresql://postgres:postgres@localhost:5432/zoo'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -203,56 +210,94 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# TODO: zwei Zookeeper anlegen\n",
|
||||
"# Zwei Zookeeper anlegen\n",
|
||||
"with Session(engine) as session:\n",
|
||||
" john = ...\n",
|
||||
" jane = ...\n",
|
||||
"\n",
|
||||
" session.add(...)\n",
|
||||
"\n",
|
||||
" john = Zookeeper(name='John Doe', email='john@example.com', specialty='Elephants')\n",
|
||||
" jane = Zookeeper(name='Jane Doe', email='jane@example.com', specialty='Giraffes')\n",
|
||||
" session.add_all([john, jane])\n",
|
||||
" session.commit()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 John Doe john@example.com Elephants\n",
|
||||
"4 Jane Doe jane@example.com Giraffes\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# TODO: Alle Zookeeper ausgeben\n",
|
||||
"# Alle Zookeeper ausgeben\n",
|
||||
"with Session(engine) as session:\n",
|
||||
" ..."
|
||||
" for zk in session.query(Zookeeper).order_by(Zookeeper.id).all():\n",
|
||||
" print(zk.id, zk.name, zk.email, zk.specialty)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# TODO: 3 Elefanten anlegen, die John betreut\n",
|
||||
"# 3 Elefanten anlegen, die John betreut\n",
|
||||
"with Session(engine) as session:\n",
|
||||
" ...\n",
|
||||
" john = session.query(Zookeeper).filter_by(name='John Doe').one()\n",
|
||||
" elephants = [\n",
|
||||
" Animal(name='Babar', species='Elephant', zookeeper=john),\n",
|
||||
" Animal(name='Dumbo', species='Elephant', zookeeper=john),\n",
|
||||
" Animal(name='Hathi', species='Elephant', zookeeper=john),\n",
|
||||
" ]\n",
|
||||
" session.add_all(elephants)\n",
|
||||
" session.commit()\n",
|
||||
"\n",
|
||||
"# TODO: 2 Giraffen anlegen, die Jane betreut\n",
|
||||
"# 2 Giraffen anlegen, die Jane betreut\n",
|
||||
"with Session(engine) as session:\n",
|
||||
" ..."
|
||||
" jane = session.query(Zookeeper).filter_by(name='Jane Doe').one()\n",
|
||||
" giraffes = [\n",
|
||||
" Animal(name='Melman', species='Giraffe', zookeeper=jane),\n",
|
||||
" Animal(name='Gloria', species='Giraffe', zookeeper=jane),\n",
|
||||
" ]\n",
|
||||
" session.add_all(giraffes)\n",
|
||||
" session.commit()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"----------------------------------------\n",
|
||||
"| Babar | Elephant | John Doe |\n",
|
||||
"| Dumbo | Elephant | John Doe |\n",
|
||||
"| Hathi | Elephant | John Doe |\n",
|
||||
"| Melman | Giraffe | Jane Doe |\n",
|
||||
"| Gloria | Giraffe | Jane Doe |\n",
|
||||
"----------------------------------------\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Liste aller Tiere mit ihren Pflegern ausgeben:\n",
|
||||
"with Session(engine) as session:\n",
|
||||
" ...\n",
|
||||
"\n",
|
||||
" animals = session.query(Animal).order_by(Animal.id).all()\n",
|
||||
" print('-' * 40)\n",
|
||||
" for animal in animals:\n",
|
||||
" print(f\"| {animal.name:<10} | {animal.species:<10} | {animal.zookeeper.name:<10} |\")\n",
|
||||
" print('-' * 40)\n",
|
||||
"# Beispielausgabe:\n",
|
||||
"#\n",
|
||||
"# ----------------------------------------\n",
|
||||
|
||||
Reference in new issue
Block a user