Skip to content

Build Python SQLite3 Command Line Tool#1

Open
dhoe wants to merge 3 commits into
claude/puppeteer-login-automation-011CUb3gHwPDqVDBwzCnfyFrfrom
claude/python-sqlite3-cli-011CUssR1fZpbD7RcD8nQ4f2
Open

Build Python SQLite3 Command Line Tool#1
dhoe wants to merge 3 commits into
claude/puppeteer-login-automation-011CUb3gHwPDqVDBwzCnfyFrfrom
claude/python-sqlite3-cli-011CUssR1fZpbD7RcD8nQ4f2

Conversation

@dhoe

@dhoe dhoe commented Nov 7, 2025

Copy link
Copy Markdown
Owner

No description provided.

Implemented a fully-featured SQLite3 command-line interface in pure Python
with only standard library dependencies. This provides a portable, compatible
alternative to the C-based sqlite3 CLI.

Features:
- Interactive REPL mode with sqlite> prompt
- All major output modes (list, csv, column, line, html, insert, quote, tabs, tcl)
- Comprehensive meta-commands (.tables, .schema, .dump, .mode, .headers, etc.)
- Command-line argument support (-csv, -column, -header, etc.)
- SQL execution from files, stdin, and command line
- Compatible with standard sqlite3 CLI workflows

The implementation uses only Python standard library modules (sqlite3, sys, os,
csv, re, io) making it highly portable and easy to deploy anywhere Python is
available.
Added proper transaction tracking to prevent auto-commit when in an
explicit transaction (BEGIN/COMMIT/ROLLBACK). This fixes compatibility
with .dump output and allows proper transaction control.

The CLI now:
- Tracks when BEGIN TRANSACTION is executed
- Disables auto-commit during explicit transactions
- Re-enables auto-commit after COMMIT, END, or ROLLBACK
- Properly handles dump file restoration with BEGIN/COMMIT blocks

This matches the behavior of the standard sqlite3 CLI tool.
Created a generalized database CLI that works with both SQLite and DuckDB
through a backend abstraction layer. This provides a unified interface for
both databases while maintaining compatibility with the sqlite3 CLI tool.

Key features:
- Backend abstraction layer with DatabaseBackend base class
- SQLite and DuckDB backend implementations
- Auto-detection of database type from file extension (.db → SQLite, .duckdb → DuckDB)
- Manual override with -db flag for explicit backend selection
- All output modes work with both databases (csv, column, line, list, etc.)
- Transaction support for both backends
- Meta-commands adapted for both databases (.tables, .schema, .dump, etc.)
- Handles differences in system catalogs (sqlite_master vs information_schema)
- Suppresses DuckDB INSERT/UPDATE/DELETE count output for CLI compatibility

The implementation maintains the same command-line interface and meta-commands
as the SQLite-only version, making it a drop-in replacement that extends
functionality to DuckDB.

Auto-detection rules:
- .db, .sqlite, .sqlite3 files → SQLite backend
- .duckdb, .ddb files → DuckDB backend
- :memory: → SQLite backend (default)
- Can be overridden with -db sqlite or -db duckdb

Dependencies:
- sqlite3 (Python standard library)
- duckdb (optional, only needed for DuckDB support)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants