mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 20:34:12 +02:00
First CLI command: sqlite-utils table_names test.db
This commit is contained in:
parent
8437fe3091
commit
fd5829b27d
4 changed files with 60 additions and 3 deletions
17
docs/cli.rst
Normal file
17
docs/cli.rst
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
.. _python_api:
|
||||||
|
|
||||||
|
================================
|
||||||
|
sqlite-utils command-line tool
|
||||||
|
================================
|
||||||
|
|
||||||
|
The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways.
|
||||||
|
|
||||||
|
Listing tables
|
||||||
|
==============
|
||||||
|
|
||||||
|
You can list the names of tables in a database using the ``table_names`` subcommand::
|
||||||
|
|
||||||
|
$ sqlite-utils table_names mydb.db
|
||||||
|
dogs
|
||||||
|
cats
|
||||||
|
chickens
|
||||||
|
|
@ -17,6 +17,7 @@ Contents
|
||||||
:maxdepth: 2
|
:maxdepth: 2
|
||||||
|
|
||||||
python-api
|
python-api
|
||||||
|
cli
|
||||||
changelog
|
changelog
|
||||||
|
|
||||||
Take a look at `this script <https://github.com/simonw/russian-ira-facebook-ads-datasette/blob/master/fetch_and_build_russian_ads.py>`_ for an example of this library in action.
|
Take a look at `this script <https://github.com/simonw/russian-ira-facebook-ads-datasette/blob/master/fetch_and_build_russian_ads.py>`_ for an example of this library in action.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,22 @@
|
||||||
import click
|
import click
|
||||||
|
import sqlite_utils
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.group()
|
||||||
def cli(*args):
|
@click.version_option()
|
||||||
click.echo(repr(args))
|
def cli():
|
||||||
|
"Commands for interacting with a SQLite database"
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.argument(
|
||||||
|
"path",
|
||||||
|
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
def table_names(path):
|
||||||
|
"""List the tables in the database"""
|
||||||
|
db = sqlite_utils.Database(path)
|
||||||
|
for name in db.table_names:
|
||||||
|
print(name)
|
||||||
|
|
|
||||||
23
tests/test_cli.py
Normal file
23
tests/test_cli.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
from sqlite_utils import cli
|
||||||
|
from click.testing import CliRunner
|
||||||
|
import pytest
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
CREATE_TABLES = """
|
||||||
|
create table Gosh (c1 text, c2 text, c3 text);
|
||||||
|
create table Gosh2 (c1 text, c2 text, c3 text);
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def db_path(tmpdir):
|
||||||
|
path = str(tmpdir / "test.db")
|
||||||
|
db = sqlite3.connect(path)
|
||||||
|
db.executescript(CREATE_TABLES)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def test_table_names(db_path):
|
||||||
|
result = CliRunner().invoke(cli.cli, ["table_names", db_path])
|
||||||
|
assert "Gosh\nGosh2" == result.output.strip()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue