aboutsummaryrefslogtreecommitdiff
path: root/09-using-sql-from-other-programming-language
diff options
context:
space:
mode:
Diffstat (limited to '09-using-sql-from-other-programming-language')
-rw-r--r--09-using-sql-from-other-programming-language/using-sql-from-other-programming-language.ipynb265
1 files changed, 203 insertions, 62 deletions
diff --git a/09-using-sql-from-other-programming-language/using-sql-from-other-programming-language.ipynb b/09-using-sql-from-other-programming-language/using-sql-from-other-programming-language.ipynb
index 96f47de..bf1b2a6 100644
--- a/09-using-sql-from-other-programming-language/using-sql-from-other-programming-language.ipynb
+++ b/09-using-sql-from-other-programming-language/using-sql-from-other-programming-language.ipynb
@@ -147,22 +147,40 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "b386073e",
- "metadata": {
- "scrolled": true
- },
+ "id": "c96b8236",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cursor.execute('DROP TABLE IF EXISTS users')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b32d8f83",
+ "metadata": {},
"outputs": [],
"source": [
"cursor.execute('''\n",
- " DROP TABLE IF EXISTS users;\n",
- "\n",
" CREATE TABLE users (\n",
" id INT PRIMARY KEY AUTO_INCREMENT,\n",
" login VARCHAR(50) UNIQUE NOT NULL,\n",
" password_salt VARBINARY(10) UNIQUE NOT NULL,\n",
" password_hash VARBINARY(130) NOT NULL\n",
" );\n",
- "\n",
+ "''')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b386073e",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [],
+ "source": [
+ "cursor.execute('''\n",
" CREATE TABLE follows (\n",
" follower_id INT,\n",
" followed_id INT,\n",
@@ -294,16 +312,23 @@
]
},
{
- "cell_type": "markdown",
- "id": "e1bd6501",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "52da9fad",
"metadata": {},
+ "outputs": [],
"source": [
- "_notes_\n",
- "\n",
- "```python\n",
- "add_user(cursor, 'pan_cratius', 'MUCH passw0rd, VERY secur!ty')\n",
- "add_user(cursor, 'EUgenia', 'hack me, LOL!')\n",
- "```"
+ "add_user(cursor, 'pan_cratius', 'MUCH passw0rd, VERY secur!ty')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ebe2e6e5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "add_user(cursor, 'EUgenia', 'hack me, LOL!')"
]
},
{
@@ -318,59 +343,118 @@
]
},
{
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f833ba50",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "connection.commit()"
+ ]
+ },
+ {
"cell_type": "markdown",
"id": "60b795f1",
"metadata": {},
"source": [
"_notes_\n",
"\n",
- "```python\n",
- "connection.commit()\n",
- "```\n",
- "\n",
"https://peps.python.org/pep-0249/#commit"
]
},
{
"cell_type": "code",
"execution_count": null,
+ "id": "6b76797b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%%sql\n",
+ "SELECT * FROM users"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
"id": "5e4569fc",
"metadata": {},
"outputs": [],
"source": [
"cursor.execute('''\n",
- " INSERT INTO follows (followed_id, follower_id, since)\n",
- " VALUES (1, 2, NOW());\n",
- " INSERT INTO follows (followed_id, follower_id, since)\n",
- " VALUES (1, 3, NOW() - INTERVAL 1 DAY);\n",
- " INSERT INTO follows (followed_id, follower_id, since)\n",
- " VALUES (3, 2, NOW() - INTERVAL 2 DAY)\n",
+ " INSERT INTO follows (follower_id, followed_id, since)\n",
+ " VALUES (2, 1, NOW());\n",
+ " INSERT INTO follows (follower_id, followed_id, since)\n",
+ " VALUES (2, 3, NOW() - INTERVAL 2 DAY)\n",
"''')"
]
},
{
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3c1b3d2b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cursor.execute('SAVEPOINT my_checkpoint')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8cc08dc2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cursor.execute('''\n",
+ " INSERT INTO follows (follower_id, followed_id, since)\n",
+ " VALUES (1, 3, NOW() - INTERVAL 2 DAY);\n",
+ "''')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "94b2ebe0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cursor.execute('ROLLBACK TO SAVEPOINT my_checkpoint')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "aead91b4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cursor.execute('COMMIT')"
+ ]
+ },
+ {
"cell_type": "markdown",
"id": "666984ce",
"metadata": {},
"source": [
"_notes_\n",
"\n",
- "```python\n",
- "cursor.execute('SAVEPOINT my_checkpoint')\n",
- "cursor.execute('''\n",
- " INSERT INTO follows (followed_id, follower_id, since)\n",
- " VALUES (3, 1, NOW() - INTERVAL 2 DAY);\n",
- "''')\n",
- "cursor.execute('ROLLBACK TO SAVEPOINT my_checkpoint')\n",
- "cursor.execute('COMMIT')\n",
- "```\n",
- "\n",
"`COMMIT` also works when passed as text."
]
},
{
"cell_type": "code",
"execution_count": null,
+ "id": "15e52fef",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%%sql\n",
+ "SELECT * FROM follows -- Is the (1, 3) pair there?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
"id": "29f29bd0",
"metadata": {
"scrolled": true
@@ -378,7 +462,7 @@
"outputs": [],
"source": [
"%%sql\n",
- "SELECT dst.login AS followed, src.login AS follower, since \n",
+ "SELECT src.login AS follower, dst.login AS followed, since \n",
"FROM users src\n",
"JOIN follows ON src.id = follows.follower_id\n",
"JOIN users dst ON follows.followed_id = dst.id"
@@ -404,13 +488,12 @@
]
},
{
- "cell_type": "markdown",
- "id": "cd73cfa9",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "01874752",
"metadata": {},
+ "outputs": [],
"source": [
- "_notes_\n",
- "\n",
- "```python\n",
"import psycopg2\n",
"\n",
"psql_connection = psycopg2.connect(\n",
@@ -421,17 +504,57 @@
" database = 'agh_it_northwind'\n",
")\n",
"\n",
- "psql_cursor = psql_connection.cursor()\n",
- "\n",
+ "psql_cursor = psql_connection.cursor()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cd4c81ef",
+ "metadata": {},
+ "outputs": [],
+ "source": [
"psql_cursor.execute(\"SELECT CAST(X'deadbeef' AS BINARY)\")\n",
- "psql_cursor.fetchall()\n",
- "```\n",
- "\n",
- "Postgres' closest type is `BYTEA`.\n",
+ "psql_cursor.fetchall()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "99076370",
+ "metadata": {},
+ "source": [
+ "_notes_\n",
"\n",
- "```python\n",
- "psql_cursor.execute(\"SELECT '\\xdeadbeef'::BYTEA)\")\n",
- "```\n",
+ "Postgres' closest type is `BYTEA`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9003ae44",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "psql_connection.rollback()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "066220c7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "psql_cursor.execute(\"SELECT '\\xdeadbeef'::BYTEA\")\n",
+ "psql_cursor.fetchall()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "cd73cfa9",
+ "metadata": {},
+ "source": [
+ "_notes_\n",
"\n",
"Note the different returned type (`memoryview` rather than `bytes`)."
]
@@ -512,15 +635,21 @@
},
{
"cell_type": "markdown",
- "id": "ee5fce14",
+ "id": "255d8a8d",
"metadata": {},
"source": [
- "_notes_\n",
- "\n",
"```\n",
"login: theodore\n",
"password: S3cur3 Pa$$word3\n",
- "```\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ee5fce14",
+ "metadata": {},
+ "source": [
+ "_notes_\n",
"\n",
"Later, use `' OR TRUE -- ` as login.\n",
"\n",
@@ -549,7 +678,9 @@
"cell_type": "code",
"execution_count": null,
"id": "8b3ba197",
- "metadata": {},
+ "metadata": {
+ "scrolled": true
+ },
"outputs": [],
"source": [
"%%sql\n",
@@ -557,16 +688,23 @@
]
},
{
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5a964e08",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%%sql\n",
+ "DEALLOCATE PREPARE get_salt"
+ ]
+ },
+ {
"cell_type": "markdown",
"id": "d775f486",
"metadata": {},
"source": [
"_notes_\n",
"\n",
- "```sql\n",
- "DEALLOCATE PREPARE get_salt\n",
- "```\n",
- "\n",
"Statements are allocated per-session, so they would get purge at the and of the session, anyway. But for a long-running session with many statements created from dynamic data it might make a difference."
]
},
@@ -666,7 +804,10 @@
"id": "e1cbcef4",
"metadata": {},
"source": [
- "_notes_\n",
+ "```\n",
+ "login: ' OR TRUE --\n",
+ "password: whatever\n",
+ "```\n",
"\n",
"```\n",
"login: theodore\n",
@@ -707,7 +848,7 @@
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
- "language": "/gnu/store/q35bxk1i5blx0rcgxphhrq9xqmgha9bz-python-3.11.11/bin/python3",
+ "language": "/gnu/store/a03fcf7xcfmd4nplyla6pmrkn4fsnaxd-python-3.12.12/bin/python3",
"name": "python3"
},
"language_info": {
@@ -720,7 +861,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.11.11"
+ "version": "3.12.12"
}
},
"nbformat": 4,