Skip to content

gh-135795: Support .tables in the sqlite3 command-line interface #135796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tanloong
Copy link
Contributor

@tanloong tanloong commented Jun 21, 2025

This adds .tables to list table names in the current database connection.

The output is made similar to that of SQLite tool to give users familiar experience. Both tables and views, whether temporary or not, are included in the output. Tables with names like sqlite_% (which is reserved by SQLite for internal use) are filtered out. Table names from the main schema are shown as is, while those from other schemas are shown with a schema-name prefix. For example, temporary table tmp is shown as temp.tmp.

The SQLite tool allows an optional TABLE argument with .tables to filter table names with LIKE pattern TABLE. This feature is not included because the SQLite standard of argument handling for dot commands is too complex, I am worried about overcomplicating the code in this single PR.

A connection has three kinds of databases: the main database; the temporary database used to store temporary tables/views etc; attached databases added to current connection using the ATTACH command. Schema-names of all these three can be obtained by PRAGMA database_list. For historical compatibility, {schema-name}.sqlite_master instead of {schema-name}.sqlite_schema is used to get table names. The CPython sqlite3 module accepts SQLite>=3.15.2, but sqlite_schema was not added in SQLite until 3.33.0.

In the select_clauses generator, the inconsistent usage of double- and single-quotes is intentional. The SQL standard requires double-quotes around identifiers and single-quotes around string literals. The double quotes in "{schema}" are to ensure that schema is recognized as an identifier even if user attaches a database with a numeric schema name, e.g., ATTACH '/path/to/db' AS 123;. This corner case is included in the test_interact_tables().

@tanloong
Copy link
Contributor Author

tanloong commented Jun 23, 2025

I happen to notice that the LIKE pattern sqlite_% actually filters out any names starting with sqlite instead of sqlite_. The underscore matches any single character and needs to be escaped. This issue was also missed in SQLite's implementation and was fixed recently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant