Guide 9 · Relational and Document Databases

sqlite3, Deep Dive

The database in the standard library: transactions, types, and a whole database in one file you can copy.

Complete 20 notebooks 20 written

Before this guide: Files, Paths and Formats.

The notebooks

Open in Colab runs a notebook in your browser, and Read shows it on GitHub without running it. Every notebook runs on its own, and the answers to its exercises are in a separate notebook, linked as Solutions.

1Why sqlite3One file, one import, and the CSV you can stop re-readingOpen in ColabReadSolutions
2Connections and Cursorsconnect, cursor, fetchone against fetchall, and the connection you forgot to closeOpen in ColabReadSolutions
3Tables and QueriesCREATE TABLE, SELECT, JOIN, and the double quotes that became a stringOpen in ColabReadSolutions
4SQL SyntaxWritten order against run order, aggregates, and the date range that lost its last dayOpen in ColabReadSolutions
5JoinsINNER, LEFT, RIGHT, FULL, CROSS and self joins, and the payroll a join counted twiceOpen in ColabReadSolutions
6ParametersQuestion marks against f-strings, and the injection an f-string invitesOpen in ColabReadSolutions
7Row Factoriessqlite3.Row, a dict factory, and rows that answer to a column nameOpen in ColabReadSolutions
8Type AffinityWhy a column declared INTEGER accepted the string, and what STRICT changesOpen in ColabReadSolutions
9Adapters and Convertersregister_adapter, PARSE_DECLTYPES, and the datetime adapter deprecated in 3.12Open in ColabReadSolutions
10Transactionscommit, rollback, and the rows that were gone when you reopened the fileOpen in ColabReadSolutions
11autocommit and isolation_levelLEGACY_TRANSACTION_CONTROL, autocommit=False, and the CREATE TABLE that rollback could not undoOpen in ColabReadSolutions
12ConstraintsUNIQUE constraint failed, and the foreign key nobody was enforcingOpen in ColabReadSolutions
13Changing a SchemaALTER TABLE ADD COLUMN, the twelve step table rebuild, and user_version as the migration number you already haveOpen in ColabReadSolutions
14executemanyexecutemany, one transaction, and the lastrowid that came back NoneOpen in ColabReadSolutions
15Indexes and Query PlansEXPLAIN QUERY PLAN, SCAN against SEARCH, and the index your WHERE clause cannot useOpen in ColabReadSolutions
16Full-Text SearchFTS5, bm25 ranking, and the MATCH you cannot use on an ordinary tableOpen in ColabReadSolutions
17Concurrency and WALdatabase is locked, busy_timeout, and the single writer WAL does not removeOpen in ColabReadSolutions
18Backup and Copyingconn.backup, VACUUM INTO, iterdump, and why copying the file mid-write is not a backupOpen in ColabReadSolutions
19A Searchable ArchiveA folder of documents into one indexed, searchable, backed-up fileOpen in ColabReadSolutions
20Everyday RequestsA college and a ledger, and the requests a developer answers with joins, aggregates and datesOpen in ColabReadSolutions