2023-07-18 19:25:22 -07:00
|
|
|
# Running queries
|
|
|
|
|
|
|
|
|
|
You can run SQL queries against a Datasette instance like this:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
dclient query https://latest.datasette.io/fixtures "select * from facetable limit 1"
|
|
|
|
|
```
|
|
|
|
|
Output:
|
|
|
|
|
```json
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"pk": 1,
|
|
|
|
|
"created": "2019-01-14 08:00:00",
|
|
|
|
|
"planet_int": 1,
|
|
|
|
|
"on_earth": 1,
|
|
|
|
|
"state": "CA",
|
|
|
|
|
"_city_id": 1,
|
|
|
|
|
"_neighborhood": "Mission",
|
|
|
|
|
"tags": "[\"tag1\", \"tag2\"]",
|
|
|
|
|
"complex_array": "[{\"foo\": \"bar\"}]",
|
|
|
|
|
"distinct_some_null": "one",
|
|
|
|
|
"n": "n1"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
Update documentation for new commands and output formats
- Update README.md with new commands summary and uv development workflow
- Add docs/exploring.md covering databases, tables, schema, rows, get
commands with filtering, sorting, pagination, and output format examples
- Add docs/writing.md covering create-table, upsert, update, delete, drop
- Update docs/queries.md with output format options (--csv, --tsv, --nl, --table)
- Update docs/inserting.md with cross-references to upsert/update
- Update docs/index.md toctree to include new pages
- Regenerate all cog --help blocks with current command signatures
https://claude.ai/code/session_01DoiJf9Gbvbw2asN9yvP6dx
2026-02-12 01:28:35 +00:00
|
|
|
## Output formats
|
|
|
|
|
|
|
|
|
|
Query results default to JSON. Use these flags for other formats:
|
|
|
|
|
|
|
|
|
|
- `--csv` — CSV
|
|
|
|
|
- `--tsv` — TSV
|
|
|
|
|
- `--nl` — Newline-delimited JSON (one JSON object per line)
|
|
|
|
|
- `--table` — Formatted text table
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
dclient query https://latest.datasette.io/fixtures \
|
|
|
|
|
"select state, count(*) as n from facetable group by state" --table
|
|
|
|
|
```
|
|
|
|
|
Output:
|
|
|
|
|
```
|
|
|
|
|
state n
|
|
|
|
|
----- --
|
|
|
|
|
CA 10
|
|
|
|
|
MI 4
|
|
|
|
|
MO 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Export as CSV:
|
|
|
|
|
```bash
|
|
|
|
|
dclient query https://latest.datasette.io/fixtures \
|
|
|
|
|
"select * from facetable limit 5" --csv > results.csv
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-18 19:25:22 -07:00
|
|
|
## dclient query --help
|
|
|
|
|
<!-- [[[cog
|
|
|
|
|
import cog
|
|
|
|
|
from dclient import cli
|
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(cli.cli, ["query", "--help"])
|
|
|
|
|
help = result.output.replace("Usage: cli", "Usage: dclient")
|
|
|
|
|
cog.out(
|
|
|
|
|
"```\n{}\n```".format(help)
|
|
|
|
|
)
|
|
|
|
|
]]] -->
|
|
|
|
|
```
|
dclient insert command (#13)
* Move making queries to own page, refs #7
* Documentation for the dclient insert feature, refs #8
* Implemented progress bar for insert, refs #8
* --pk option
* --ignore and --replace for insert, refs #8
* --batch-size option, refs #8
* First insert test, using a mock - refs #8
* insert test that exercises against an in-memory Datasette instance, refs #8
* Insert tests now cover an error case, refs #8
* Tests for --ignore and --replace, refs #8
* Tests for different formats, refs #8
* Test for --no-detect-types
* Support for --encoding, refs #8
2023-07-24 16:22:39 -07:00
|
|
|
Usage: dclient query [OPTIONS] URL_OR_ALIAS SQL
|
2023-07-18 19:25:22 -07:00
|
|
|
|
|
|
|
|
Run a SQL query against a Datasette database URL
|
|
|
|
|
|
|
|
|
|
Returns a JSON array of objects
|
|
|
|
|
|
2024-02-25 11:24:46 -08:00
|
|
|
Example usage:
|
|
|
|
|
|
|
|
|
|
dclient query \
|
|
|
|
|
https://datasette.io/content \
|
|
|
|
|
'select * from news limit 10'
|
|
|
|
|
|
2023-07-18 19:25:22 -07:00
|
|
|
Options:
|
2024-02-27 20:52:15 -08:00
|
|
|
--token TEXT API token
|
|
|
|
|
-v, --verbose Verbose output: show HTTP request
|
Update documentation for new commands and output formats
- Update README.md with new commands summary and uv development workflow
- Add docs/exploring.md covering databases, tables, schema, rows, get
commands with filtering, sorting, pagination, and output format examples
- Add docs/writing.md covering create-table, upsert, update, delete, drop
- Update docs/queries.md with output format options (--csv, --tsv, --nl, --table)
- Update docs/inserting.md with cross-references to upsert/update
- Update docs/index.md toctree to include new pages
- Regenerate all cog --help blocks with current command signatures
https://claude.ai/code/session_01DoiJf9Gbvbw2asN9yvP6dx
2026-02-12 01:28:35 +00:00
|
|
|
--csv Output as CSV
|
|
|
|
|
--tsv Output as TSV
|
|
|
|
|
--json Output as JSON (default)
|
|
|
|
|
--nl Output as newline-delimited JSON
|
|
|
|
|
--table Output as text table
|
2024-02-27 20:52:15 -08:00
|
|
|
--help Show this message and exit.
|
2023-07-18 19:25:22 -07:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
<!-- [[[end]]] -->
|