From 2e34d6480b9baf0a30686a5f5169dd9fb66027f1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 1 Aug 2026 19:10:00 -0500 Subject: [PATCH] Add Experiment With SQLite Queries In Memory as a Python TIL --- README.md | 3 +- ...xperiment-with-sqlite-queries-in-memory.md | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 python/experiment-with-sqlite-queries-in-memory.md diff --git a/README.md b/README.md index 5ef49d8..f43523b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1847 TILs and counting..._ +_1848 TILs and counting..._ See some of the other learning resources I work on: @@ -1093,6 +1093,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Dunder Methods](python/dunder-methods.md) - [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md) - [Enable Pyright Type Checking In Cursor](python/enable-pyright-type-checking-in-cursor.md) +- [Experiment With SQLite Queries In Memory](python/experiment-with-sqlite-queries-in-memory.md) - [Force Remaining Arguments To Be Named](python/force-remaining-arguments-to-be-named.md) - [Get Absolute Seconds From `timedelta` Object](python/get-absolute-seconds-from-timedelta-object.md) - [Get Quotient And Remainder In One Operation](python/get-quotient-and-remainder-in-one-operation.md) diff --git a/python/experiment-with-sqlite-queries-in-memory.md b/python/experiment-with-sqlite-queries-in-memory.md new file mode 100644 index 0000000..caf5465 --- /dev/null +++ b/python/experiment-with-sqlite-queries-in-memory.md @@ -0,0 +1,44 @@ +# Experiment With SQLite Queries In Memory + +Instead of connecting +[`sqlite3`](https://docs.python.org/3/library/sqlite3.html) to a real file +(database) like so: + +```python +db_file: Path = data_dir / "sessions.db" +conn: Connection = sqlite3.connect(db_file) +``` + +I can point it to `":memory:"`: + +```python +conn: Connection = sqlite3.connect(":memory:") +``` + +One way this can be useful is when experimenting with some DDL (schema-modifying +SQL statements), e.g. creating a table, adding a column, and so forth. + +```python +>>> import sqlite3 +>>> conn = sqlite3.connect(":memory:") +>>> conn.execute(""" +... create table projects ( +... id integer primary key, +... name text not null unique, +... created_at text not null default (datetime('now')), +... updated_at text not null default (datetime('now')) +... ); +... """) + +>>> conn.execute("insert into projects (name) values ('TIL'), ('py-vmt'), ('Pool League Pro');") + +>>> result = conn.execute("select * from projects;") +>>> rows = result.fetchall() +>>> rows +[(1, 'TIL', '2026-08-01 23:59:56', '2026-08-01 23:59:56'), (2, 'py-vmt', '2026-08-01 23:59:56', '2026-08-01 23:59:56'), (3, 'Pool League Pro', '2026-08-01 23:59:56', '2026-08-01 23:59:56')] +``` + +This does not create any sort of on-disk version of the database. Two separate +connections created this way will be independent in-memory database instances. +This is technically more of [a SQLite feature](https://sqlite.org/inmemorydb.html) than a Python one, but it was in a +Python context that I found this most useful.