{"slug": "mapping-sqlite-result-columns-back-to-their-source-table-column", "title": "Mapping SQLite result columns back to their source `table.column`", "summary": "Simon Willison is researching methods to map SQLite query result columns back to their source table and column names, aiming to enhance Datasette's ability to render additional metadata for arbitrary SQL queries. He tasked Claude Code with finding solutions, which identified approaches using the apsw library, ctypes to access SQLite's internal C function, and analysis of EXPLAIN output.", "body_md": "**Research:** [Mapping SQLite result columns back to their source `table.column`](https://github.com/simonw/research/tree/main/sqlite-column-provenance#readme)\n\nIt would be neat if arbitrary SQL queries in [Datasette](https://datasette.io/) could be rendered with additional information based on which columns from which tables were included in the results.\n\nTo build that, we would need to be able to look at a SQL query like `select users.name, orders.total from users join orders on orders.user_id = users.id`\n\nand programmatically identify the `table.column`\n\nfor each result - navigating not just joins but also more complex syntax like CTEs.\n\nI decided to set Claude Code (Opus 4.8, since Fable is currently [banned by the US government](https://simonwillison.net/2026/Jun/13/us-government-directive-to-suspend-access/)) on the problem. It found several promising solutions - one using [apsw](https://github.com/rogerbinns/apsw), another that uses `ctypes`\n\nto access the SQLite `sqlite3_column_table_name()`\n\n[C function](https://sqlite.org/c3ref/column_database_name.html) (which is not otherwise exposed to Python), and one using clever interrogation of the output of `EXPLAIN`\n\n.", "url": "https://wpnews.pro/news/mapping-sqlite-result-columns-back-to-their-source-table-column", "canonical_source": "https://simonwillison.net/2026/Jun/13/sqlite-column-provenance/#atom-everything", "published_at": "2026-06-13 23:05:00+00:00", "updated_at": "2026-06-14 00:29:45.020906+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools", "large-language-models"], "entities": ["Datasette", "SQLite", "Claude Code", "Simon Willison", "apsw", "ctypes"], "alternates": {"html": "https://wpnews.pro/news/mapping-sqlite-result-columns-back-to-their-source-table-column", "markdown": "https://wpnews.pro/news/mapping-sqlite-result-columns-back-to-their-source-table-column.md", "text": "https://wpnews.pro/news/mapping-sqlite-result-columns-back-to-their-source-table-column.txt", "jsonld": "https://wpnews.pro/news/mapping-sqlite-result-columns-back-to-their-source-table-column.jsonld"}}