AppliedMachineAndDeepLearni.../CPS_Database_Basics.ipynb

364 lines
9.1 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "92fb5068-0669-43e3-9799-9dca9ffe6638",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"%%html\n",
"<style>\n",
"table {float:left}\n",
"</style>"
]
},
{
"cell_type": "markdown",
"id": "11174c4d-dcb5-49d8-96fc-b83ad35a6193",
"metadata": {},
"source": [
"<div>\n",
"<img src=\"https://cps.unileoben.ac.at/wp/CPS_Logo_Black.png\" width=\"260\"/>\n",
"</div>\n",
"Chair of Cyber-Physical-Systems, Austria"
]
},
{
"cell_type": "markdown",
"id": "a04ead7b-4fce-4ea2-9501-d60a112aadcb",
"metadata": {},
"source": [
"<style>\n",
"td, th {\n",
" border: none!important;\n",
"}\n",
"</style>\n",
"\n",
"| Credentials | |\n",
"|----|---|\n",
"|Host | Montanuniversitaet Leoben |\n",
"|Web | https://cps.unileoben.ac.at |\n",
"|Mail | cps@unileoben.ac.at |\n",
"|Authors | Vedant Dave, Fotis Lygerakis, Linus Nwankwo, Melanie Neubauer, Nikolaus Feith and Elmar Rueckert|\n",
"|Corresponding Authors | melanie.neubauer@unileoben.ac.at, rueckert@unileoben.ac.at |\n",
"|Last edited | 28.09.2023 |\n"
]
},
{
"cell_type": "markdown",
"id": "35a0d7a0",
"metadata": {},
"source": [
"# Python Database Basics"
]
},
{
"cell_type": "markdown",
"id": "4b05e025-9843-410e-80cd-f2db24148da8",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-success\">\n",
"This is a tutorial on the basics of using databases in Python."
]
},
{
"cell_type": "markdown",
"id": "b9edce42-35e4-4dcd-886c-8d19a30bfdf0",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Content\n",
"***\n",
"[1. Motivation for learning Python?](#sec:whypython) \n",
"\n",
"[2. Database Overview](#sec:overview)\n",
"\n",
"[3. Database Example in Python](#sec:example)"
]
},
{
"cell_type": "markdown",
"id": "c4e0ceef-663a-46c3-9e52-6cd6c942422f",
"metadata": {},
"source": [
"# <a id=\"sec:whypython\">1. Motivation for Learning Python?</a>\n",
"....\n",
" \n",
"<div>\n",
"<img src=\"https://cps.unileoben.ac.at/wp/statistics_2023_programming_language.png\" width=\"600\"/>\n",
"</div>\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ba97999c",
"metadata": {},
"source": [
"# <a id=\"sec:overview\">2. Database Overview?</a> \n",
"\n",
"| | MariaDB | InfluxDB | PostgreSQL | Graphite | TimescaleDB |\n",
"|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|\n",
"| Type | Relational| Time Series | Relational | Time Series | Time Series |\n",
"| Language | SQL | InfluxQL | SQL | Graphite Query | --- |\n",
"| Scalable | Good | Good | Good | Lose Performance when saving a big amound of data | Very Good |\n",
"| Replication | Yes | Yes | Yes | No | Yes |\n",
"| High-Availability | Yes | Yes | Yes | No | Yes |\n",
"| Data-Transformation | Limited | Yes | Yes | Yes | Yes |\n",
"| Community-Support Python | Good | OK | --- | --- | --- |\n",
"| Compression | Yes | Yes | Yes | Yes | Yes |\n",
"| Partitioning | Yes | Yes | Yes | No | Yes |\n",
"| Image Saving | BLOB or BYTEA | No | BLOB or BYTEA | No | BLOB or BYTEA |\n",
"| Python | Mysql-connector-python | Influxdb-python | psycopg2 | No | psycopg2, timescaledb |\n",
"| Integrated Graphics | No (connect with Grafana) | Yes | No | Yes | Yes |\n"
]
},
{
"cell_type": "markdown",
"id": "66f200c3",
"metadata": {},
"source": [
"# <a id=\"sec:example\">3. Database Example in Python</a> \n",
"## Create new Database (open existing Database)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "974c33a4",
"metadata": {},
"outputs": [],
"source": [
"# Import\n",
"import sqlite3"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "06024e10-c790-4e7f-b4d2-717f2f8c1826",
"metadata": {},
"outputs": [],
"source": [
"# Set the Path of the Database\n",
"database_path = 'example_database.db'"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "3e2e0d5c-86c3-4f58-8ec5-28888c1fef49",
"metadata": {},
"outputs": [],
"source": [
"# Create connection to the SQLite-Database\n",
"db_conn = sqlite3.connect(database_path)\n",
"db_cursor = db_conn.cursor()"
]
},
{
"cell_type": "markdown",
"id": "fd3a2b81-9207-4a5e-8807-a1427c2f87b1",
"metadata": {},
"source": [
"## Create Table in Database"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "437ca01a-89ea-4e99-af04-c2b19ebf0f3a",
"metadata": {},
"outputs": [],
"source": [
"# Create a Table in Database\n",
"db_cursor.execute('''\n",
" CREATE TABLE IF NOT EXISTS students (\n",
" m_number INTEGER PRIMARY KEY,\n",
" name TEXT,\n",
" age INTEGER\n",
" )\n",
" ''')\n",
"db_conn.commit()"
]
},
{
"cell_type": "markdown",
"id": "88e7e045-8ab1-498d-b928-3e0fe51ca624",
"metadata": {},
"source": [
"## Create Entry in Database"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "9f74ae84-7411-42e9-89ab-b7c4e12559bf",
"metadata": {},
"outputs": [],
"source": [
"# Create two entries for students\n",
"student_1 = (123456789, 'Josef', 20)\n",
"student_2 = (987654321, 'Sissi', 33)\n",
"\n",
"db_cursor.execute(f'''INSERT INTO students VALUES ({student_1[0]}, '{student_1[1]}', {student_1[2]})''')\n",
"db_cursor.execute(f'''INSERT INTO students VALUES ({student_2[0]}, '{student_2[1]}', {student_2[2]})''')\n",
"db_conn.commit()"
]
},
{
"cell_type": "markdown",
"id": "d5b87f79-6285-432d-8f4d-7ad2730e7b11",
"metadata": {},
"source": [
"## Show Table from Database"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "ec07f074-f508-4a5c-b65f-392d28c73ce1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(987654321, 'Sissi', 33)\n"
]
}
],
"source": [
"# Print each row of the table students in the database\n",
"for row in db_cursor.execute('''SELECT * FROM students'''):\n",
" print(row)"
]
},
{
"cell_type": "markdown",
"id": "d7e72081-859f-4c19-9393-92c2f6e72b57",
"metadata": {},
"source": [
"## Search for Entry in Table"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "b29460b0-d7e4-47d5-b65e-eb3aac352648",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"# Search for Student with the m_number 123456789\n",
"db_cursor.execute(f'''SELECT name, age FROM students WHERE m_number = {123456789}''')\n",
"result = db_cursor.fetchall()\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "53a5f8d1-3444-48f1-80a4-87db8cb3858c",
"metadata": {},
"source": [
"## Delete Entry from Table"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "6248e45a-6cd9-42bb-9f30-ee979a8865a3",
"metadata": {},
"outputs": [],
"source": [
"# Delete Student with the m_number 123456789 from the Database\n",
"db_cursor.execute(f'''DELETE FROM students WHERE m_number = {123456789}''')\n",
"db_conn.commit()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "5c675bc4-6528-424a-88df-0023cc79eb29",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(987654321, 'Sissi', 33)\n"
]
}
],
"source": [
"# Now you can show the table again\n",
"# Print each row of the table students in the database\n",
"for row in db_cursor.execute('''SELECT * FROM students'''):\n",
" print(row)"
]
},
{
"cell_type": "markdown",
"id": "119cc9ce-b361-4ccc-892b-98cb635ad6d5",
"metadata": {},
"source": [
"## Close Database Connection"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "24cacc8f-ba78-4768-a4cb-425c9c9ca7f3",
"metadata": {},
"outputs": [],
"source": [
"# After using, always close the connection to the database\n",
"db_conn.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5946f811-d132-48cb-9409-e0bbf12c9b52",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}