# Shell 指令

> Turso 互動式 SQL Shell 中可用的點指令
> Canonical HTML: https://tursodb.cn/zh-tw/docs/sql-reference/cli/shell-commands/
> Upstream Source (MIT): https://docs.turso.tech/sql-reference/cli/shell-commands
> Language: zh-TW

---
# Shell 指令

點指令（Dot commands）是互動式 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

列出資料庫中的所有資料表，可選擇性地依模式（pattern）篩選。

```
.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 語句；若未提供引數，則顯示所有資料表的 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

切換回顯（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（Virtual Database Engine，虛擬資料庫引擎）的 opcode 及其說明。可選擇性地依 opcode 名稱篩選。

```
.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

列出可用的 Virtual File System（虛擬檔案系統）模組。

```
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">NOTE</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` | 開啟 | 使用逗號作為欄位分隔符號，並使用換行作為記錄分隔符號 |
| `--skip N` | 0 | 略過前 N 列（適用於略過標題列） |
| `--verbose` | 關閉 | 在匯入期間印出進度資訊 |

```
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">NOTE</div><div class="doc-callout-body">

僅能載入 Turso 原生的擴充功能。不支援 SQLite 的 `.so`/`.dll` 可載入擴充功能。關於可用的擴充功能，請參閱 [Extensions](/zh-tw/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 中為空操作（no-op）。

```
.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
```
