database.serialize([dbName])


  • dbName <string> 要序列化的数据库名称。这可以是 'main'(默认的主数据库)或任何已通过 ATTACH DATABASE 添加的其他数据库。默认值: 'main'
  • 返回:<Uint8Array> 数据库的二进制表示。

将数据库序列化为二进制表示,返回为 Uint8Array。这对于保存、克隆或传输内存中的数据库非常有用。此方法是 sqlite3_serialize() 的封装器。

🌐 Serializes the database into a binary representation, returned as a Uint8Array. This is useful for saving, cloning, or transferring an in-memory database. This method is a wrapper around sqlite3_serialize().

import { DatabaseSync } from 'node:sqlite';

const db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
db.exec("INSERT INTO t VALUES (1, 'hello')");
const buffer = db.serialize();
console.log(buffer.length); // Prints the byte length of the databaseconst { DatabaseSync } = require('node:sqlite');

const db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE t(key INTEGER PRIMARY KEY, value TEXT)');
db.exec("INSERT INTO t VALUES (1, 'hello')");
const buffer = db.serialize();
console.log(buffer.length); // Prints the byte length of the database