# Shell 命令

> Turso 交互式 SQL Shell 中可用的点命令
> Canonical HTML: https://tursodb.cn/zh-cn/docs/sql-reference/cli/shell-commands/
> Upstream Source (MIT): https://docs.turso.tech/sql-reference/cli/shell-commands
> Language: zh-CN

---
# Shell 命令

点命令是交互式 shell 中可用的特殊命令。它们以句点（`.`）开头，且不需要末尾分号。

```
tursodb> .help
```

## 数据库与导航

### .open

打开数据库文件，可选择指定 VFS 后端。

```
.open <PATH> [VFS]
```

```
tursodb> .open mydata.db
tursodb> .open mydata.db memory
```

### .quit

退出 shell。别名：`.q`、`.qu`、`.qui`。

```
tursodb> .quit
```

### .exit

以可选返回码退出 shell。别名：`.ex`、`.exi`。

```
.exit [CODE]
```

```
tursodb> .exit
tursodb> .exit 1
```

### .cd

更改当前工作目录。

```
.cd <DIRECTORY>
```

```
tursodb> .cd /tmp
```

## 架构检查

### .tables

列出数据库中的所有表，可选择按模式过滤。

```
.tables [PATTERN]
```

```sql
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE departments (id INTEGER PRIMARY KEY, name TEXT);
```

```
tursodb> .tables
employees
departments

tursodb> .tables emp%
employees
```

### .schema

显示表的 CREATE 语句；如果未提供参数，则显示所有表。

```
.schema [TABLE]
```

```
tursodb> .schema employees
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT);

tursodb> .schema
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE departments (id INTEGER PRIMARY KEY, name TEXT);
```

### .indexes

显示索引名称，可选择按表过滤。

```
.indexes [TABLE]
```

```sql
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, department TEXT);
CREATE INDEX idx_dept ON employees(department);
CREATE INDEX idx_name ON employees(name);
```

```
tursodb> .indexes
idx_dept
idx_name

tursodb> .indexes employees
idx_dept
idx_name
```

### .databases

列出所有已附加的数据库。

```
tursodb> .databases
main: /path/to/mydata.db r/w
```

## 输出控制

### .mode

设置输出显示模式。

```
.mode <MODE>
```

| 模式 | 描述 |
| - | - |
| `pretty` | 带边框的表格（默认） |
| `list` | 管道符分隔的值 |
| `line` | 每行一个列，并包含列名 |

```
tursodb> .mode list
tursodb> SELECT 1 AS a, 2 AS b;
1|2

tursodb> .mode line
tursodb> SELECT 1 AS a, 2 AS b;
a = 1
b = 2

tursodb> .mode pretty
tursodb> SELECT 1 AS a, 2 AS b;
┌───┬───┐
│ a │ b │
├───┼───┤
│ 1 │ 2 │
└───┴───┘
```

### .headers

在 `list` 模式下开启或关闭列标题。

```
.headers <on|off>
```

```
tursodb> .mode list
tursodb> .headers on
tursodb> SELECT 1 AS x, 2 AS y;
x|y
1|2

tursodb> .headers off
tursodb> SELECT 1 AS x, 2 AS y;
1|2
```

### .nullvalue

设置 `list` 模式下 NULL 值显示的字符串。

```
.nullvalue <STRING>
```

```
tursodb> .mode list
tursodb> .nullvalue [NULL]
tursodb> SELECT 1 AS a, NULL AS b;
1|[NULL]
```

### .output

将查询输出重定向到文件。不带参数或使用 `stdout` 调用可将输出恢复到终端。

```
.output [PATH]
```

```
tursodb> .output results.txt
tursodb> SELECT * FROM employees;
tursodb> .output
tursodb> -- Output is back to the terminal
```

### .echo

切换回显模式。开启后，每条 SQL 语句在执行前都会打印出来。

```
.echo <on|off>
```

```
tursodb> .echo on
tursodb> SELECT 42;
SELECT 42;
┌────┐
│ 42 │
├────┤
│ 42 │
└────┘
```

### .show

显示当前 shell 设置。

```
tursodb> .show
Settings:
Output mode: pretty
DB: mydata.db
Output: STDOUT
Null value:
CWD: /home/user
Echo: off
Headers: off
```

## 性能与诊断

### .timer

切换查询计时。开启后，每次查询后显示执行时间和 I/O 统计信息。

```
.timer <on|off>
```

```
tursodb> .timer on
tursodb> SELECT 42;
42
Command stats:
----------------------------
total: 442 us (this includes parsing/coloring of cli app)

query execution stats:
----------------------------
Execution: avg=4 us, total=8 us
I/O: No samples available
```

### .stats

显示数据库统计信息。使用 `on`/`off` 切换在每次查询后自动显示，或使用 `--reset` 清除计数器。

```
.stats [on|off] [--reset]
```

```
tursodb> .stats
Connection Metrics:
  Total statements:     3
  High-water marks:
    Max VM steps:       19
    Max rows read:      1

Aggregate Statistics:
Statement Metrics:
  Row Operations:
    Rows read:        1
    Rows written:     2
  Execution:
    VM steps:         48
    Instructions:     47
  ...
```

### .opcodes

显示 VDBE（虚拟数据库引擎）操作码及其说明。可选择按操作码名称过滤。

```
.opcodes [OPCODE]
```

```
tursodb> .opcodes Integer
Integer
-------
The 64-bit integer value P1 is written into register P2. This is different
from SQLite, where this opcode is used for 32-bit integers
```

### .vfslist

列出可用的虚拟文件系统模块。

```
tursodb> .vfslist
Available VFS modules:
memory
syscall
```

## 数据导入与导出

### .dump

将整个数据库输出为可重建该数据库的 SQL 语句。

```
tursodb> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO "employees" VALUES(1,'Alice');
INSERT INTO "employees" VALUES(2,'Bob');
COMMIT;
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

`.dump` 的输出可以通过管道传给另一个 Turso 实例，以重建数据库：

```bash
tursodb original.db ".dump" | tursodb -q clone.db
```

</div></div>


### .import

将文件中的数据导入表中。

```
.import [--csv] [--skip N] [--verbose] <FILE> <TABLE>
```

| 选项 | 默认值 | 描述 |
| - | - | - |
| `--csv` | on | 使用逗号作为字段分隔符，使用换行符作为记录分隔符 |
| `--skip N` | 0 | 跳过前 N 行（适合跳过表头行） |
| `--verbose` | off | 导入过程中打印进度信息 |

```
tursodb> CREATE TABLE people (name TEXT, age INTEGER);
tursodb> .import --csv --skip 1 data.csv people
tursodb> SELECT * FROM people;
┌─────────┬─────┐
│ name    │ age │
├─────────┼─────┤
│ Alice   │  30 │
├─────────┼─────┤
│ Bob     │  25 │
├─────────┼─────┤
│ Charlie │  35 │
└─────────┴─────┘
```

给定 CSV 文件 `data.csv`：

```csv
name,age
Alice,30
Bob,25
Charlie,35
```

### .clone

将当前数据库克隆到新文件。

```
.clone <OUTPUT_FILE>
```

```
tursodb> .clone backup.db
employees... done
```

### .read

从文件中执行 SQL 语句。

```
.read <PATH>
```

```
tursodb> .read setup.sql
```

### .load

加载扩展库。

```
.load <PATH>
```

```
tursodb> .load ./liblimbo_regexp
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

只能加载 Turso 原生扩展。不支持 SQLite 的 `.so`/`.dll` 可加载扩展。可用扩展参见 [扩展](/zh-cn/docs/sql-reference/extensions/) 文档。

</div></div>


## 调试

### .dbtotxt

以十六进制格式显示原始数据库页面内容。适用于调试存储层级问题。

```
.dbtotxt [--page PAGE_NO]
```

```
tursodb> .dbtotxt --page 1
| size 8192 pagesize 4096 filename :memory:
| page 1 offset 0
|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
|     16: 10 00 02 02 00 40 20 20 00 00 00 01 00 00 00 02   .....@  ........
...
```

### .dbconfig

打印或设置数据库配置标志。目前在 Turso 中为空操作。

```
.dbconfig [CONFIG] [on|off]
```

## 文档

### .manual

显示 Turso 功能的内置手册页。不带参数调用可列出所有可用手册。

```
.manual [PAGE]
```

别名：`.man`。

```
tursodb> .manual
# Turso Manual Pages

Available manuals:
  custom-types    Custom Types for STRICT Tables
  cdc             Change Data Capture
  encryption      At-Rest Database Encryption
  vector          Vector Search
  materialized-views  Live Materialized Views
  mcp             Model Context Protocol server

tursodb> .manual vector
```
