Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c743049
Add .tables for sqlite3 command-line interface
tanloong Jun 21, 2025
0caa466
blurb add
tanloong Jun 21, 2025
a84165d
fix news entry
tanloong Jun 21, 2025
f7a7781
fix CONCAT function not found on old version SQLite
tanloong Jun 21, 2025
a26f75c
remove unnecessary collection of schema name with tuple(); update hel…
tanloong Jun 23, 2025
473e78d
Fix false filtering out table names with "sqlite" prefix instead of "…
tanloong Jun 23, 2025
ce129af
Merge branch 'main' into sqlite3-cli-dot-tables
tanloong Oct 9, 2025
d54da10
add .tables to CLI_COMMANDS in _completer.py
tanloong Oct 9, 2025
9924bc5
fix misuse of ESCAPE keyword
tanloong Oct 9, 2025
3fcc661
Add .tables for sqlite3 command-line interface
tanloong Jun 21, 2025
61de02c
blurb add
tanloong Jun 21, 2025
e14da1e
fix news entry
tanloong Jun 21, 2025
f0ace80
fix CONCAT function not found on old version SQLite
tanloong Jun 21, 2025
459bc02
remove unnecessary collection of schema name with tuple(); update hel…
tanloong Jun 23, 2025
288ac54
Fix false filtering out table names with "sqlite" prefix instead of "…
tanloong Jun 23, 2025
2fbc54a
add .tables to CLI_COMMANDS in _completer.py
tanloong Oct 9, 2025
ade6c0f
fix misuse of ESCAPE keyword
tanloong Oct 9, 2025
6deb0ea
Add .indexes
tanloong Feb 8, 2026
b335d7b
Merge branch 'sqlite3-cli-dot-tables' of https://github.com/tanloong/…
tanloong Mar 4, 2026
a813c92
Merge branch 'main' into sqlite3-cli-dot-tables
tanloong Mar 4, 2026
ae1bb61
Merge branch 'main' into sqlite3-cli-dot-tables
tanloong May 2, 2026
3534d57
Merge branch 'main' into sqlite3-cli-dot-tables
tanloong May 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,34 @@ def runsource(self, source, filename="<input>", symbol="single"):
return False
# Remember to update CLI_COMMANDS in _completer.py
if source[0] == ".":
match source[1:].strip():
cmd_name = source[1:].strip()
match cmd_name:
case "tables" | "indices" | "indexes":
schema_names = (row[1]
for row in self._cur.execute("PRAGMA database_list"))
where_clause = ("""WHERE type IN ('table', 'view')
AND name NOT LIKE 'sqlite^_%' ESCAPE '^'"""
if cmd_name == "tables" else "WHERE type='index'"
)
select_clauses = (f"""SELECT
CASE '{schema}'
WHEN 'main' THEN name
ELSE '{schema}.' || name
END
FROM "{schema}".sqlite_master {where_clause}"""
for schema in schema_names
)
command = " UNION ALL ".join(select_clauses) + " ORDER BY 1"
for row in self._cur.execute(command):
print(row[0])
case "version":
print(sqlite3.sqlite_version)
case "help":
t = theme.syntax
print(f"Enter SQL code or one of the below commands, and press enter.\n\n"
f"{t.builtin}.tables{t.reset} Print names of tables\n"
f"{t.builtin}.indexes{t.reset} Print names of indexes\n"
f"{t.builtin}.indices{t.reset} Print names of indices\n"
f"{t.builtin}.version{t.reset} Print underlying SQLite library version\n"
f"{t.builtin}.help{t.reset} Print this help message\n"
f"{t.builtin}.quit{t.reset} Exit the CLI, equivalent to CTRL-D\n")
Expand Down
2 changes: 1 addition & 1 deletion Lib/sqlite3/_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
except ImportError:
SQLITE_KEYWORDS = ()

CLI_COMMANDS = ('.quit', '.help', '.version')
CLI_COMMANDS = ('.quit', '.help', '.version', '.tables', '.indices', '.indexes')

_completion_matches = []

Expand Down
54 changes: 54 additions & 0 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,60 @@ def test_interact_version(self):
self.assertEqual(out.count(self.PS2), 0)
self.assertIn(sqlite3.sqlite_version, out)

def test_interact_tables(self):
out, err = self.run_cli(commands=(
"CREATE TABLE table_ (id INTEGER);",
"CREATE TABLE sqlitee (id INTEGER);",
"CREATE TEMP TABLE table_ (id INTEGER);",
"CREATE VIEW view_ AS SELECT 1;",
"CREATE TEMP VIEW view_ AS SELECT 1;",
"ATTACH ':memory:' AS attach_;",
"CREATE TABLE attach_.table_ (id INTEGER);",
"CREATE VIEW attach_.view_ AS SELECT 1;",
"ATTACH ':memory:' AS 123;",
"CREATE TABLE \"123\".table_ (id INTEGER);",
"CREATE VIEW \"123\".view_ AS SELECT 1;",
".tables",
))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 13)
self.assertEqual(out.count(self.PS2), 0)
tables = ("123.table_",
"123.view_",
"attach_.table_",
"attach_.view_",
"sqlitee",
"table_",
"temp.table_",
"temp.view_",
"view_")
self.assertEqual("\n".join(tables), out.replace(self.PS1, "").strip())

def test_interact_indexes(self):
out, err = self.run_cli(commands=(
"CREATE TABLE table_ (id INTEGER);",
"CREATE INDEX idx_table_ ON table_ (id);",
"CREATE TEMP TABLE temp_table (id INTEGER);",
"CREATE INDEX temp.idx_temp_table_ ON temp_table (id);",
"ATTACH ':memory:' AS attach_;",
"CREATE TABLE attach_.attach_table (id INTEGER);",
"CREATE INDEX attach_.idx_attach_table ON attach_table (id);",
".indexes",
".indices",
))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 10)
self.assertEqual(out.count(self.PS2), 0)
expected = ("attach_.idx_attach_table",
"idx_table_",
"temp.idx_temp_table_",
"attach_.idx_attach_table",
"idx_table_",
"temp.idx_temp_table_")
self.assertEqual("\n".join(expected), out.replace(self.PS1, "").strip())

def test_interact_empty_source(self):
out, err = self.run_cli(commands=("", " "))
self.assertIn(self.MEMORY_DB_MSG, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support ``.tables`` in the :mod:`sqlite3` command-line interface.
Loading