From 647453caf8463c627bec97eb913d3a960660e3dc Mon Sep 17 00:00:00 2001 From: "Jangberry (Nomad-Debian)" <matt2001@hotmail.fr> Date: Wed, 13 Dec 2023 14:56:24 +0100 Subject: [PATCH] Avancement --- .gitignore | 2 + BE_SGBD_LIBRARY.ddl | 1 + fill.ipynb | 114 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 fill.ipynb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ecbfcaa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.venv \ No newline at end of file diff --git a/BE_SGBD_LIBRARY.ddl b/BE_SGBD_LIBRARY.ddl index a918fe9..777ff56 100644 --- a/BE_SGBD_LIBRARY.ddl +++ b/BE_SGBD_LIBRARY.ddl @@ -12,6 +12,7 @@ -- Database Section -- ________________ +DROP DATABASE IF EXISTS MAIN; create database MAIN; USE MAIN; diff --git a/fill.ipynb b/fill.ipynb new file mode 100644 index 0000000..7cb2339 --- /dev/null +++ b/fill.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SGBD Library\n", + "\n", + "## Usage\n", + "\n", + "1. Run a SQL server (for instance `docker run --name sgdb --env MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 -p 3306:3306 mariadb:latest`)\n", + "\n", + "2. *You may need to* install the following package: `apt install libmariadb-dev`\n", + "\n", + "3. Then, run the following code:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: mariadb in ./.venv/lib/python3.10/site-packages (1.1.8)\n", + "Requirement already satisfied: packaging in ./.venv/lib/python3.10/site-packages (from mariadb) (23.2)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install mariadb\n", + "import mariadb" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "mariaDB = mariadb.connect(\n", + " user=\"root\",\n", + " host=\"172.17.0.2\", # IP of the container\n", + " # host=\"172.0.0.1\",\n", + " port=3306,\n", + ")\n", + "\n", + "cur = mariaDB.cursor()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This code will apply the dll:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "def exec_sql_file(cursor, sql_file):\n", + " statement = \"\"\n", + "\n", + " for line in open(sql_file):\n", + " if line.startswith('--'):\n", + " continue\n", + " if not line.strip().endswith(';'):\n", + " statement = statement + line\n", + " else: # when you get a line ending in ';' then exec statement and reset for next statement\n", + " statement = statement + line\n", + " try:\n", + " cursor.execute(statement)\n", + " except (mariadb.OperationalError, mariadb.ProgrammingError) as e:\n", + " print(\"\\n[WARN] MySQLError during execute statement \\n\\tArgs: '%s'\" % (str(e.args)))\n", + "\n", + " statement = \"\"\n", + "\n", + "exec_sql_file(cur, \"BE_SGBD_LIBRARY.ddl\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab