# SQLite

> 英文标题：SQLite
> 官网原文：https://typeorm.io/docs/drivers/sqlite
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 28 块

---

<!-- EN -->
# SQLite

<!-- ZH -->
# SQLite

<!-- EN -->
## Installation

<!-- ZH -->
## 安装

<!-- EN -->
* for **Better SQLite**:

<!-- ZH -->
* 对于 **Better SQLite**：

<!-- EN -->
```shell
npm install better-sqlite3
```

<!-- ZH -->
```shell
npm install better-sqlite3
```

<!-- EN -->
* for **sql.js**:

<!-- ZH -->
* 对于 **sql.js**：

<!-- EN -->
```shell
npm install sql.js
```

<!-- ZH -->
```shell
npm install sql.js
```

<!-- EN -->
* for **Capacitor**, **Cordova**, **Expo**, **NativeScript** and **React Native**, check the [supported platforms](https://typeorm.io/docs/help/supported-platforms).

<!-- ZH -->
* 对于 **Capacitor**、**Cordova**、**Expo**、**NativeScript** 和 **React Native**，请查看[支持的平台](https://typeorm.io/docs/help/supported-platforms)。

<!-- EN -->
## Data Source Options

<!-- ZH -->
## 数据源选项

<!-- EN -->
See [Data Source Options](https://typeorm.io/docs/data-source/data-source-options) for the common data source options.

<!-- ZH -->
通用数据源选项请参阅[数据源选项](https://typeorm.io/docs/data-source/data-source-options)。

<!-- EN -->
### `better-sqlite3` data source options

<!-- ZH -->
### `better-sqlite3` 数据源选项

<!-- EN -->
* `database` - Database path. For example, `"mydb.sqlite"`.
* `enableWAL` - Enables WAL mode (default `false`). See [SQLite WAL mode](https://www.sqlite.org/wal.html).
* `fileMustExist` - If the database does not exist, an Error will be thrown instead of creating a new file. Does not affect in-memory or readonly connections (default `false`).
* `key` - Encryption key for SQLCipher.
* `nativeBinding` - Relative or absolute path to the native addon (`better_sqlite3.node`).
* `prepareDatabase` - Function to run before a database is used in typeorm. You can access the original better-sqlite3 Database object here.
* `readonly` - Open the database connection in readonly mode (default `false`).
* `statementCacheSize` - Cache size of the SQLite statement to speed up queries (default `100`).
* `timeout` - The number of milliseconds to wait when executing queries on a locked database, before throwing a SQLITE_BUSY error (default `5000`).
* `verbose` - A function that gets called with every SQL string executed by the database connection.

<!-- ZH -->
* `database` - 数据库路径。例如 `"mydb.sqlite"`。
* `enableWAL` - 启用 WAL 模式（默认 `false`）。参见 [SQLite WAL 模式](https://www.sqlite.org/wal.html)。
* `fileMustExist` - 如果数据库不存在，将抛出 Error 而不是创建新文件。对内存连接或只读连接无影响（默认 `false`）。
* `key` - SQLCipher 的加密密钥。
* `nativeBinding` - 原生插件（`better_sqlite3.node`）的相对或绝对路径。
* `prepareDatabase` - 在 typeorm 中使用数据库之前要运行的函数。你可以在这里访问原始的 better-sqlite3 Database 对象。
* `readonly` - 以只读模式打开数据库连接（默认 `false`）。
* `statementCacheSize` - SQLite 语句缓存大小，用于加速查询（默认 `100`）。
* `timeout` - 在被锁定的数据库上执行查询时，在抛出 SQLITE_BUSY 错误之前等待的毫秒数（默认 `5000`）。
* `verbose` - 一个函数，数据库连接执行的每条 SQL 字符串都会调用它。

<!-- EN -->
### `sql.js` data source options

<!-- ZH -->
### `sql.js` 数据源选项

<!-- EN -->
* `database`: The raw UInt8Array database that should be imported.
* `sqlJsConfig`: Optional initialize config for sql.js.
* `autoSave`: Enable automatic persistence of database changes, requires either `location` or `autoSaveCallback`. When set to `true`, every change is saved to the file system (Node.js) or to `localStorage`/`indexedDB` (browser) if `location` is specified, or the `autoSaveCallback` is invoked otherwise.
* `autoSaveCallback`: A function that gets called when changes to the database are made and `autoSave` is enabled. The function gets a `UInt8Array` that represents the database.
* `location`: The file location to load and save the database to.
* `useLocalForage`: Enables the usage of the [localforage library](https://github.com/localForage/localForage) to save and load the database asynchronously from the indexedDB instead of using the synchrony local storage methods in a browser environment. The localforage node module needs to be added to your project, and the localforage.js should be imported in your page.

<!-- ZH -->
* `database`：要导入的原始 UInt8Array 数据库。
* `sqlJsConfig`：sql.js 的可选初始化配置。
* `autoSave`：启用数据库变更的自动持久化，需要提供 `location` 或 `autoSaveCallback`。设为 `true` 时，如果指定了 `location`，每次变更都会保存到文件系统（Node.js）或 `localStorage`/`indexedDB`（浏览器）；否则会调用 `autoSaveCallback`。
* `autoSaveCallback`：当数据库发生变更且 `autoSave` 已启用时调用的函数。该函数会收到一个表示数据库的 `UInt8Array`。
* `location`：加载和保存数据库的文件位置。
* `useLocalForage`：启用 [localforage 库](https://github.com/localForage/localForage)，在浏览器环境中从 indexedDB 异步保存和加载数据库，而不是使用同步的 local storage 方法。你需要把 localforage node 模块加入项目，并在页面中引入 localforage.js。

<!-- EN -->
### `capacitor` data source options

<!-- ZH -->
### `capacitor` 数据源选项

<!-- EN -->
* `database` - Database name (capacitor-sqlite will add the suffix `SQLite.db`)
* `driver` - The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`.
* `mode` - Set the mode for database encryption: "no-encryption" | "encryption" | "secret" | "newsecret"
* `version` - Database version
* `journalMode` - The SQLite journal mode (optional)

<!-- ZH -->
* `database` - 数据库名（capacitor-sqlite 会加上后缀 `SQLite.db`）
* `driver` - capacitor-sqlite 实例。例如 `new SQLiteConnection(CapacitorSQLite)`。
* `mode` - 设置数据库加密模式："no-encryption" | "encryption" | "secret" | "newsecret"
* `version` - 数据库版本
* `journalMode` - SQLite 日志模式（可选）

<!-- EN -->
### `cordova` data source options

<!-- ZH -->
### `cordova` 数据源选项

<!-- EN -->
* `database` - Database name
* `location` - Where to save the database. See [cordova-sqlite-storage](https://github.com/litehelpers/Cordova-sqlite-storage#opening-a-database) for options.

<!-- ZH -->
* `database` - 数据库名
* `location` - 数据库保存位置。选项参见 [cordova-sqlite-storage](https://github.com/litehelpers/Cordova-sqlite-storage#opening-a-database)。

<!-- EN -->
### `expo` data source options

<!-- ZH -->
### `expo` 数据源选项

<!-- EN -->
* `database` - Name of the database. For example, "mydb".
* `driver` - The Expo SQLite module. Optional — TypeORM loads `expo-sqlite` automatically. Pass explicitly only when you need a custom instance (for example, a patched fork).

<!-- ZH -->
* `database` - 数据库名。例如 "mydb"。
* `driver` - Expo SQLite 模块。可选——TypeORM 会自动加载 `expo-sqlite`。只有在你需要自定义实例（例如打过补丁的 fork）时才显式传入。

<!-- EN -->
### `nativescript` data source options

<!-- ZH -->
### `nativescript` 数据源选项

<!-- EN -->
* `database` - Database name

<!-- ZH -->
* `database` - 数据库名

<!-- EN -->
### `react-native` data source options

<!-- ZH -->
### `react-native` 数据源选项

<!-- EN -->
* `database` - Database name
* `location` - Where to save the database. See [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage#opening-a-database) for options.

<!-- ZH -->
* `database` - 数据库名
* `location` - 数据库保存位置。选项参见 [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage#opening-a-database)。

<!-- EN -->
## Column Types

<!-- ZH -->
## 列类型

<!-- EN -->
`int`, `int2`, `int8`, `integer`, `tinyint`, `smallint`, `mediumint`, `bigint`, `decimal`, `numeric`, `float`, `double`, `real`, `double precision`, `datetime`, `varying character`, `character`, `native character`, `varchar`, `nchar`, `nvarchar2`, `unsigned big int`, `boolean`, `blob`, `text`, `clob`, `date`, `json`, `jsonb`

<!-- ZH -->
`int`、`int2`、`int8`、`integer`、`tinyint`、`smallint`、`mediumint`、`bigint`、`decimal`、`numeric`、`float`、`double`、`real`、`double precision`、`datetime`、`varying character`、`character`、`native character`、`varchar`、`nchar`、`nvarchar2`、`unsigned big int`、`boolean`、`blob`、`text`、`clob`、`date`、`json`、`jsonb`

<!-- EN -->
TypeORM supports both `json` and `jsonb` types in SQLite:

<!-- ZH -->
TypeORM 在 SQLite 中同时支持 `json` 和 `jsonb` 类型：

<!-- EN -->
* `json` is stored as `TEXT`.
* `jsonb` is stored as SQLite's binary JSON format. TypeORM automatically wraps values with the `jsonb()` function during persistence and with the `json()` function during retrieval for transparent support and better performance.

<!-- ZH -->
* `json` 存储为 `TEXT`。
* `jsonb` 存储为 SQLite 的二进制 JSON 格式。为了提供透明支持并获得更好的性能，TypeORM 会在持久化时自动用 `jsonb()` 函数包裹值，在读取时自动用 `json()` 函数包裹。

<!-- EN -->
JSONB support requires SQLite 3.45.0 or newer. When using the `jsonb` column type, TypeORM will use the `jsonb` type in your database schema, which SQLite handles as a binary `BLOB` internally.

<!-- ZH -->
JSONB 支持需要 SQLite 3.45.0 或更高版本。使用 `jsonb` 列类型时，TypeORM 会在你的数据库结构中使用 `jsonb` 类型，而 SQLite 在内部把它当作二进制 `BLOB` 处理。
