> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tinycloud.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL

> Query and manage SQLite databases in TinyCloud

Use `tc sql` when your app stores structured data in a TinyCloud space and you want SQLite semantics from the terminal.

## Core commands

```bash theme={null}
tc sql query "SELECT * FROM notes ORDER BY id"
tc sql execute "INSERT INTO notes (body) VALUES (?)" --params '["ship docs"]'
tc sql export --output notes.db
tc sql copy --from-db source --to-db destination
```

## Targeting

`--db` picks the database name inside the current space. `--space` targets a different owned space.

```bash theme={null}
tc sql query "SELECT count(*) FROM events" --db analytics
tc sql query "SELECT count(*) FROM conversation" --space applications --db xyz.tinycloud.listen/conversations
```

## Parameters

Pass bind parameters as a JSON array.

```bash theme={null}
tc sql query "SELECT * FROM notes WHERE id = ?" --params '[1]'
tc sql execute "UPDATE notes SET body = ? WHERE id = ?" --params '["edited", 1]'
```

## Outputs

* `query` prints a table in the terminal and JSON with `--json`
* `execute` returns `changes` and `lastInsertRowId`
* `export` writes a `.db` file locally
* `copy` moves rows between databases and can run as a dry run

## Practical order

1. Create or host the target space
2. Run `tc sql execute` to materialize the schema
3. Query with `tc sql query`
4. Export only when you need a local copy

<Tip>
  If a SQL command fails because the target space is not hosted, host the owned space first and retry the command.
</Tip>
