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 到 SQLiteSQLite 到 JavaScript
NULL<null><null>
INTEGER<number><bigint><number><bigint> (可配置)
REAL<number><number>
TEXT<string><string>
BLOB<TypedArray><DataView><Uint8Array>

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