JavaScript 和 SQLite 之间的类型转换
🌐 Type conversion between JavaScript and SQLite
当 Node.js 向 SQLite 写入或从 SQLite 读取时,有必要在 JavaScript 数据类型和 SQLite 的 数据类型 之间进行转换。由于 JavaScript 支持的数据类型比 SQLite 多,因此只支持 JavaScript 类型的一个子集。尝试将不支持的数据类型写入 SQLite 将导致异常。
🌐 When Node.js writes to or reads from SQLite, it is necessary to convert between JavaScript data types and SQLite's data types. Because JavaScript supports more data types than SQLite, only a subset of JavaScript types are supported. Attempting to write an unsupported data type to SQLite will result in an exception.
| 存储类型 | JavaScript 到 SQLite | SQLite 到 JavaScript |
|---|---|---|
NULL | <null> | <null> |
INTEGER | <number>、<bigint> 或 <boolean> | <number> 或 <bigint> (可配置) |
REAL | <number> | <number> |
TEXT | <string> | <string> |
BLOB | <TypedArray>、<DataView>、<ArrayBuffer> 或 <SharedArrayBuffer> | <Uint8Array> |
布尔值写作 INTEGER 值 1 和 0。像其他 INTEGER 值一样,它们默认会被读取为 <number>,当启用读取 BigInt 时会被读取为 <bigint> 值(1n 和 0n)。写入一个不适合有符号 64 位整数的 <bigint> 会抛出 ERR_INVALID_ARG_VALUE 错误。
🌐 Booleans are written as the INTEGER values 1 and 0. Like any other
INTEGER value, they are read back as <number> by default, or as <bigint>
values (1n and 0n) when reading BigInts is enabled. Writing a <bigint> that
does not fit in a signed 64-bit integer throws an ERR_INVALID_ARG_VALUE
error.
从 SQLite 读取值的 API 有一个配置选项,用于确定 INTEGER 值在 JavaScript 中是转换为 number 还是 bigint,例如语句的 readBigInts 选项和用户自定义函数的 useBigIntArguments 选项。如果 Node.js 从 SQLite 读取一个超出 JavaScript 安全整数 范围的 INTEGER 值,且未启用读取 BigInts 的选项,那么将抛出 ERR_OUT_OF_RANGE 错误。
🌐 APIs that read values from SQLite have a configuration option that determines
whether INTEGER values are converted to number or bigint in JavaScript,
such as the readBigInts option for statements and the useBigIntArguments
option for user-defined functions. If Node.js reads an INTEGER value from
SQLite that is outside the JavaScript safe integer range, and the option to
read BigInts is not enabled, then an ERR_OUT_OF_RANGE error will be thrown.