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

> 该引擎支持将数据导入到 SQLite 和从 SQLite 导出数据，并支持直接从 ClickHouse 查询 SQLite 表。

# SQLite 表引擎

export const CloudNotSupportedBadge = () => {
  return <a href="https://clickhouse.com/docs/products/cloud/guides/cloud-compatibility#list-of-unsupported-features" className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud 不支持此功能
        </a>;
};

<CloudNotSupportedBadge />

该引擎支持将数据导入到 SQLite 和从 SQLite 导出数据，也支持直接从 ClickHouse 查询 SQLite 表。

## 创建表

```sql theme={null}
    CREATE TABLE [IF NOT EXISTS] [db.]table_name
    (
        name1 [type1],
        name2 [type2], ...
    ) ENGINE = SQLite('db_path', 'table')
```

**引擎参数**

* `db_path` — SQLite 数据库文件的路径。
* `table` — SQLite 数据库中的表名，或原样传递给 SQLite 的查询 (请参见[传递查询而非表名](#passing-a-query)) 。

## 传递查询而非表名

`table` 参数不一定非得是表名，也可以是一个原样传递给 SQLite 的 `SELECT` 查询。表的结构将根据查询结果自动推断。该查询既可以写成子查询，也可以封装在 `query` 函数中：

```sql theme={null}
CREATE TABLE sqlite_table ENGINE = SQLite('sqlite.db', (SELECT col1, col2 FROM table1 WHERE col2 > 1));
CREATE TABLE sqlite_table ENGINE = SQLite('sqlite.db', query('SELECT col1, col2 FROM table1 WHERE col2 > 1'));
```

这样的表是只读的：不允许向其中执行 `INSERT`。[`sqlite`](/zh/reference/functions/table-functions/sqlite) 表函数也支持相同的语法。

<Note>
  子查询形式 `(SELECT ...)` 会先由 ClickHouse 解析并重新序列化，再发送给 SQLite。因此，它必须是有效的 ClickHouse SQL。若要传递 ClickHouse 无法解析的 SQLite 特有语法，请使用 `query('...')` 形式，其文本会原样发送给 SQLite。

  周围 ClickHouse 查询中的任何外层 `WHERE`、`LIMIT`、聚合等**都不会**下推到传入的查询中——而是会在拉取完整查询结果后由 ClickHouse 应用。若要限制从 SQLite 读取的数据，请将过滤条件放在传入的查询内部。启用 [`external_table_strict_query = 1`](/zh/reference/settings/session-settings/external-table#external_table_strict_query) 时，作用于该表列的外层过滤条件会直接报错拒绝，而不是在本地应用，因为它无法下推到传入的查询中。检查涵盖顶层 `WHERE` 谓词以及顶层 `AND` 的每个合取项。作用于该表列的 `PREWHERE` 不属于此设置的适用情形：此表引擎不支持 `PREWHERE`，无论该设置如何，此类查询都会因 `ILLEGAL_PREWHERE` 被拒绝。仅在过滤条件本可被下推的情况下执行检查：当该表是查询中唯一的表、位于 `INNER JOIN` 的任一侧，或位于外连接的保留侧 (`LEFT JOIN` 的左侧、`RIGHT JOIN` 的右侧) 。在 `LEFT`/`RIGHT JOIN` 的非保留侧以及 `FULL JOIN` 的任一侧，不会下推任何内容，也不会进行任何检查，因此即使在严格模式下，作用于该表列的过滤条件也会在连接后于本地应用。在执行检查的情况下，引用周围查询中其他已连接表的谓词不会被下推，也不会纳入检查，无论它仅引用连接侧，还是在一个非 `AND` 表达式中与该表混合引用 (例如 `OR`) ；此类谓词会保留其通常的 ClickHouse 求值位置 (连接后的 `WHERE`、连接前的 `PREWHERE`) ，且不会被拒绝。
</Note>

## 数据类型支持

当在表定义中显式指定 ClickHouse 列类型时，可从 SQLite 的 TEXT 列中解析出以下 ClickHouse 类型：

* [Date](/zh/reference/data-types/date), [Date32](/zh/reference/data-types/date32)
* [DateTime](/zh/reference/data-types/datetime), [DateTime64](/zh/reference/data-types/datetime64)
* [UUID](/zh/reference/data-types/uuid)
* [Enum8, Enum16](/zh/reference/data-types/enum)
* [Decimal32, Decimal64, Decimal128, Decimal256](/zh/reference/data-types/decimal)
* [FixedString](/zh/reference/data-types/fixedstring)
* 所有整数类型 ([UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64](/zh/reference/data-types/int-uint))
* [Float32, Float64](/zh/reference/data-types/float)

有关默认类型映射，请参见 [SQLite database engine](/zh/reference/engines/database-engines/sqlite#data_types-support)。

## 使用示例

显示创建 SQLite 表的查询：

```sql theme={null}
SHOW CREATE TABLE sqlite_db.table2;
```

```text theme={null}
CREATE TABLE SQLite.table2
(
    `col1` Nullable(Int32),
    `col2` Nullable(String)
)
ENGINE = SQLite('sqlite.db','table2');
```

返回该表中的数据：

```sql theme={null}
SELECT * FROM sqlite_db.table2 ORDER BY col1;
```

```text theme={null}
┌─col1─┬─col2──┐
│    1 │ text1 │
│    2 │ text2 │
│    3 │ text3 │
└──────┴───────┘
```

**另请参见**

* [SQLite](/zh/reference/engines/database-engines/sqlite) 引擎
* [sqlite](/zh/reference/functions/table-functions/sqlite) 表函数
