mirror of
https://github.com/jbranchaud/til
synced 2026-09-02 09:41:47 +00:00
Add Experiment With SQLite Queries In Memory as a Python TIL
This commit is contained in:
@@ -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).
|
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:
|
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)
|
- [Dunder Methods](python/dunder-methods.md)
|
||||||
- [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.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)
|
- [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)
|
- [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 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)
|
- [Get Quotient And Remainder In One Operation](python/get-quotient-and-remainder-in-one-operation.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'))
|
||||||
|
... );
|
||||||
|
... """)
|
||||||
|
<sqlite3.Cursor object at 0x105d6e2c0>
|
||||||
|
>>> conn.execute("insert into projects (name) values ('TIL'), ('py-vmt'), ('Pool League Pro');")
|
||||||
|
<sqlite3.Cursor object at 0x105d6e340>
|
||||||
|
>>> 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.
|
||||||
Reference in New Issue
Block a user