{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "4d2f6331-3b21-45d8-a1d4-0f31471463ce", "metadata": {}, "outputs": [], "source": [ "import csv\n", "from DATA225utils import make_connection, dataframe_query" ] }, { "cell_type": "code", "execution_count": null, "id": "295c34b0-4708-4eca-9e24-b281eedfb98d", "metadata": {}, "outputs": [], "source": [ "conn = make_connection('titanic.ini')\n", "cursor = conn.cursor()" ] }, { "cell_type": "markdown", "id": "50909cf3-bcd7-4b02-a5b7-c67c24f9f0c9", "metadata": {}, "source": [ "## Create the passenger table." ] }, { "cell_type": "code", "execution_count": null, "id": "e6e622d8-8f3f-43b4-9999-74e2d73521b9", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP TABLE IF EXISTS passengers')\n", "\n", "cursor.execute( \"\"\"\n", " CREATE TABLE passengers\n", " (\n", " name VARCHAR(32) NOT NULL,\n", " survived VARCHAR(3) NOT NULL,\n", " sex VARCHAR(6) NOT NULL,\n", " age VARCHAR(16) NOT NULL,\n", " passenger_class VARCHAR(3),\n", " PRIMARY KEY (name, age, passenger_class)\n", " )\n", " \"\"\"\n", " )" ] }, { "cell_type": "markdown", "id": "c81f8d1a-1732-4d99-96bf-7adeb94294f2", "metadata": {}, "source": [ "## Load the passenger table." ] }, { "cell_type": "code", "execution_count": null, "id": "3c3cc688-1e76-4b69-8a38-30d9c8427e69", "metadata": {}, "outputs": [], "source": [ "sql = ( \"\"\"\n", " INSERT INTO passengers\n", " VALUES (%s, %s, %s, %s, %s)\n", " \"\"\"\n", " )\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ebcb7af9-1870-49dc-8de5-e65d4c79110f", "metadata": {}, "outputs": [], "source": [ "first = True\n", "\n", "with open('TitanicSurvival.csv', newline='') as csv_file:\n", " data = csv.reader(csv_file, delimiter=',', quotechar='\"')\n", " \n", " for row in data:\n", " if not first:\n", " cursor.execute(sql, row)\n", " \n", " first = False\n", " \n", "conn.commit()" ] }, { "cell_type": "markdown", "id": "829ae112-aacf-4ea2-8df4-bea2de1474df", "metadata": {}, "source": [ "## Create the young_men_survivors table." ] }, { "cell_type": "code", "execution_count": null, "id": "05d30dd3-5bfe-47ce-8086-92334d8fdf4e", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP TABLE IF EXISTS young_men_survivors')\n", "\n", "sql = \"\"\"\n", " CREATE TABLE young_men_survivors\n", " (\n", " name VARCHAR(32) NOT NULL UNIQUE,\n", " age DOUBLE NOT NULL,\n", " class INT NOT NULL,\n", " PRIMARY KEY(name)\n", " )\n", " \"\"\"\n", "\n", "cursor.execute(sql)" ] }, { "cell_type": "markdown", "id": "08eebdcd-1c6c-460e-a77d-243350c86fcf", "metadata": {}, "source": [ "## Load and display the young_men_survivors table." ] }, { "cell_type": "code", "execution_count": null, "id": "53e3671d-b155-485d-a90c-e977cd7e2be4", "metadata": {}, "outputs": [], "source": [ "sql = \"\"\"\n", " INSERT INTO young_men_survivors\n", " SELECT name, age,\n", " CASE \n", " WHEN passenger_class = '1st' THEN 1\n", " WHEN passenger_class = '2nd' THEN 2\n", " WHEN passenger_class = '3rd' THEN 3\n", " ELSE 0\n", " END AS class\n", " FROM passengers\n", " WHERE sex = 'male'\n", " AND survived = 'yes'\n", " AND age >= 21\n", " AND age < 25\n", " \"\"\"\n", "\n", "cursor.execute(sql)\n", "conn.commit()" ] }, { "cell_type": "code", "execution_count": null, "id": "77e3380d-2ee6-4769-a794-48a4a07bda31", "metadata": {}, "outputs": [], "source": [ "_, df = dataframe_query(conn, \"\"\"\n", " SELECT *\n", " FROM young_men_survivors\n", " ORDER BY class\n", " \"\"\"\n", " )\n", "display(df)" ] }, { "cell_type": "code", "execution_count": null, "id": "3d50dd07-7174-47a2-aba2-999ae303c8ef", "metadata": {}, "outputs": [], "source": [ "conn.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }