> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-vortex-format.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> SQLite 格式文档

# SQLite

| 输入 | 输出 | 别名 |
| - | - | - |
| ✔ | ✔ | |

<h2 id="description">
  描述
</h2>

`SQLite` 格式用于读写 SQLite 数据库文件。

输出时，ClickHouse 会将查询结果写入 SQLite 数据库中的单个表；输入时，ClickHouse 会从 SQLite 数据库中读取单个表。

对于本地常规文件，ClickHouse 会直接打开 SQLite 数据库文件；对于其他输入流和输出流，ClickHouse 会先将 SQLite 数据库物化到内存中。

输入时，ClickHouse 默认读取 SQLite 数据库中的第一个表，可通过 `input_format_sqlite_table_name` 设置进行更改。输出时，默认表名为 `table`，可通过 `output_format_sqlite_table_name` 设置进行更改。

<h2 id="example-usage">
  使用示例
</h2>

```bash theme={null}
clickhouse-client --query="SELECT number, toString(number) AS s FROM numbers(10) FORMAT SQLite" > data.sqlite
clickhouse-local --input-format SQLite --structure "number UInt64, s String" --query "SELECT * FROM table" < data.sqlite
```

<h2 id="data-types-matching">
  数据类型匹配
</h2>

当 ClickHouse 以 `SQLite` 格式写入数据时，会创建一个 SQLite 表，并使用以下声明类型：

| ClickHouse 数据类型 | SQLite 声明类型 |
| - | - |
| `UInt64`, `Int128`, `UInt128`, `Int256`, `UInt256` | `TEXT` |
| `Bool`, `Int8`, `Int16`, `Int32`, `Int64`, `UInt8`, `UInt16`, `UInt32` | `INTEGER` |
| 浮点类型 | `REAL` |
| 其他类型 | `TEXT` |

对于 `Float32` 和 `Float64` 列，非 `NaN` 值以 SQLite 原生存储类写入，`NaN` 值则以 ClickHouse 文本序列化形式写入。`Bool` 和普通整数值同样以 SQLite 原生存储类写入。宽整数和复杂类型以 ClickHouse 文本序列化形式写入。`NULL` 值写入为 SQLite `NULL`。

ClickHouse 从 SQLite 输入推断 schema 时，采用与 [SQLite 数据库引擎](/zh/engines/database-engines/sqlite#data_types-support)相同的映射规则：

| SQLite 声明类型 | ClickHouse 推断类型 |
| - | - |
| 类型名称包含 `INT` | `Int64` |
| `REAL`, `FLOAT`, `DOUBLE` | `Float64` |
| 其他类型，包括 `TEXT` 和 `BLOB` | `String` |

默认情况下，如果对应的 SQLite 列允许 `NULL`，ClickHouse 会将推断类型包装为 `Nullable`；`NOT NULL` 列则保持为非 `Nullable`。可以通过 [`schema_inference_make_columns_nullable`](/zh/reference/settings/formats/schema-inference#schema_inference_make_columns_nullable) 设置更改此行为。

schema 推断不会保留原始的 ClickHouse 数据类型。例如，`Bool` 会写入为 SQLite `INTEGER`，再被推断为 `Int64`；而 `Date`、`DateTime`、`Decimal`、`UUID`、`IPv4`、`IPv6`、`Enum`、`Array`、`Tuple` 和 `Map` 会写入为 SQLite `TEXT`，再被推断为 `String`。

如果显式指定了 ClickHouse 表结构，浮点值将按 SQLite `REAL` 值读取，遇到 `NaN` 时回退为按文本读取。其他 SQLite 值则按文本读取，并解析为所请求的 ClickHouse 类型。

<h2 id="format-settings">
  格式设置
</h2>

| 设置 | 描述 | 默认值 |
| - | - | - |
| [`input_format_sqlite_table_name`](/zh/reference/settings/formats/input-format#input_format_sqlite_table_name) | 从 SQLite 输入中读取数据的表名。若为空，则使用第一个表。 | `''` |
| [`output_format_sqlite_table_name`](/zh/reference/settings/formats/output-format#output_format_sqlite_table_name) | SQLite 输出中使用的表名。 | `'table'` |
