{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "6d67e1ab-ab9c-49e3-857c-b290bb142dc1", "metadata": {}, "outputs": [], "source": [ "from pandas import DataFrame\n", "from DATA225utils import make_connection, dataframe_query" ] }, { "cell_type": "code", "execution_count": null, "id": "b28ca256-370b-4a3e-a1a2-f42c62eb30a1", "metadata": {}, "outputs": [], "source": [ "def display_table(conn, table):\n", " \"\"\"\n", " Use the cursor to return the contents of the database table\n", " in a dataframe.\n", " \"\"\"\n", " sql = f\"SELECT * FROM {table}\"\n", " _, df = dataframe_query(conn, sql)\n", " \n", " return df" ] }, { "cell_type": "code", "execution_count": null, "id": "659ceb6a-bfd5-44c0-b60c-577f4deba6c0", "metadata": {}, "outputs": [], "source": [ "def display_database(database_name, config_file):\n", " \"\"\"\n", " Use the configuration file to display the tables\n", " of the database named database_name.\n", " \"\"\"\n", " conn = make_connection(config_file=config_file)\n", " cursor = conn.cursor()\n", " \n", " print('-'*(len('DATABASE ' + database_name)))\n", " print(f'DATABASE {database_name}')\n", " print('-'*(len('DATABASE ' + database_name)))\n", " \n", " # Get the names of the database tables.\n", " cursor.execute('SHOW TABLES');\n", " results = cursor.fetchall()\n", " tables = [result[0] for result in results]\n", " \n", " # Display the contents of each table in a dataframe.\n", " for table in tables:\n", " print()\n", " print(table)\n", " \n", " df = display_table(conn, table)\n", " display(df)\n", " \n", " cursor.close()\n", " conn.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "42dd2fe0-330a-49c7-81be-34d8f0c27a5c", "metadata": {}, "outputs": [], "source": [ "for db_pair in [ ('zagi_sales', 'zagi-sales.ini'),\n", " ('zagi_facilities', 'zagi-facilities.ini'),\n", " ('zagi_customers', 'zagi-customers.ini'),\n", " ('zagi_warehouse', 'zagi-warehouse.ini')\n", " ]:\n", " display_database(db_pair[0], db_pair[1])" ] }, { "cell_type": "markdown", "id": "2dea6bcc-75fb-4402-bcef-4b26ea99fef7", "metadata": {}, "source": [ "#### (c) Copyright 2023 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "id": "cddb9b1e-3185-49b9-9708-5bdee01c2750", "metadata": {}, "outputs": [], "source": [] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }