Created
June 1, 2021 16:28
-
-
Save joefutrelle/ae97eccd5afef17d65b03735a46a5122 to your computer and use it in GitHub Desktop.
Working with SQLite in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "24e04ce5", | |
"metadata": {}, | |
"source": [ | |
"These examples are taken from the [Software Carpentry](https://software-carpentry.org/) lesson for [Databases and SQL](https://swcarpentry.github.io/sql-novice-survey/), specifically the section on [working with databases in Python](https://swcarpentry.github.io/sql-novice-survey/10-prog/index.html).\n", | |
"\n", | |
"To run these examples, download the [survey.db](https://swcarpentry.github.io/sql-novice-survey/files/survey.db) SQLite database file and place it in the same folder as this notebook." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "78a228e5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(-49.85, -128.57)\n", | |
"(-47.15, -126.72)\n", | |
"(-48.87, -123.4)\n" | |
] | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"connection = sqlite3.connect(\"survey.db\")\n", | |
"cursor = connection.cursor()\n", | |
"cursor.execute(\"SELECT Site.lat, Site.long FROM Site;\")\n", | |
"results = cursor.fetchall()\n", | |
"for r in results:\n", | |
" print(r)\n", | |
"cursor.close()\n", | |
"connection.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "96c739ca", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Full name for dyer: William Dyer\n" | |
] | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"def get_name(database_file, person_id):\n", | |
" query = \"SELECT personal || ' ' || family FROM Person WHERE id='\" + person_id + \"';\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query)\n", | |
" results = cursor.fetchall()\n", | |
" cursor.close()\n", | |
" connection.close()\n", | |
"\n", | |
" return results[0][0]\n", | |
"\n", | |
"print(\"Full name for dyer:\", get_name('survey.db', 'dyer'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "d7f25187", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Full name for dyer: William Dyer\n" | |
] | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"def get_name(database_file, person_id):\n", | |
" query = \"SELECT personal || ' ' || family FROM Person WHERE id=?;\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, [person_id])\n", | |
" results = cursor.fetchall()\n", | |
" cursor.close()\n", | |
" connection.close()\n", | |
"\n", | |
" return results[0][0]\n", | |
"\n", | |
"print(\"Full name for dyer:\", get_name('survey.db', 'dyer'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "5160cfd4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "IndexError", | |
"evalue": "list index out of range", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-4-2481a760cc61>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0madd_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'survey.db'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'barrett'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Mary'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Barrett'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;31m# Check it exists\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Full name for barrett:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mget_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'survey.db'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'barrett'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-4-2481a760cc61>\u001b[0m in \u001b[0;36mget_name\u001b[0;34m(database_file, person_id)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mconnection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mresults\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;31m# Insert a new name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mIndexError\u001b[0m: list index out of range" | |
] | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"def add_name(database_file, new_person):\n", | |
" query = \"INSERT INTO Person (id, personal, family) VALUES (?, ?, ?);\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, list(new_person))\n", | |
" cursor.close()\n", | |
" connection.close()\n", | |
"\n", | |
"\n", | |
"def get_name(database_file, person_id):\n", | |
" query = \"SELECT personal || ' ' || family FROM Person WHERE id=?;\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, [person_id])\n", | |
" results = cursor.fetchall()\n", | |
" cursor.close()\n", | |
" connection.close()\n", | |
"\n", | |
" return results[0][0]\n", | |
"\n", | |
"# Insert a new name\n", | |
"add_name('survey.db', ('barrett', 'Mary', 'Barrett'))\n", | |
"# Check it exists\n", | |
"print(\"Full name for barrett:\", get_name('survey.db', 'barrett'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "7ed24475", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Full name for barrett: Mary Barrett\n" | |
] | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"def add_name(database_file, new_person):\n", | |
" query = \"INSERT INTO Person (id, personal, family) VALUES (?, ?, ?);\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, list(new_person))\n", | |
" cursor.close()\n", | |
" connection.commit()\n", | |
" connection.close()\n", | |
"\n", | |
"\n", | |
"def get_name(database_file, person_id):\n", | |
" query = \"SELECT personal || ' ' || family FROM Person WHERE id=?;\"\n", | |
"\n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, [person_id])\n", | |
" results = cursor.fetchall()\n", | |
" cursor.close()\n", | |
" connection.close()\n", | |
"\n", | |
" return results[0][0]\n", | |
"\n", | |
"# Insert a new name\n", | |
"add_name('survey.db', ('barrett', 'Mary', 'Barrett'))\n", | |
"# Check it exists\n", | |
"print(\"Full name for barrett:\", get_name('survey.db', 'barrett'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "1cf6ed8a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def remove_name(database_file, person_id):\n", | |
" query = 'DELETE from Person where id=?;'\n", | |
" \n", | |
" connection = sqlite3.connect(database_file)\n", | |
" cursor = connection.cursor()\n", | |
" cursor.execute(query, [person_id])\n", | |
" cursor.close()\n", | |
" connection.commit()\n", | |
" connection.close()\n", | |
"\n", | |
"# remove the name we added to put the database back into its initial state\n", | |
"remove_name('survey.db', 'barrett')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment