Thin wrappers around SQLite's optional sqlite3_serialize / sqlite3_deserialize APIs. Guarded by compile-time checks so code can adapt to builds without serialization support.

Use cases

  • Great for snapshotting tiny databases in test fixtures—serialize once, deserialize into :memory: for each test.
  • Pair with connection_pool to warm up worker connections using the same pre-seeded image.
  • Throw informative database_exception errors when SQLite was compiled without serialization, so you can feature-detect gracefully.

Example usage

if (!sqlite::serialization_supported()) {
    throw std::runtime_error("Ship a build compiled with SQLITE_ENABLE_DESERIALIZE");
}

auto seed = sqlite::serialize(db, "main");

sqlite::connection mem{":memory:"};
sqlite::deserialize(mem, seed, "main");

sqlite::query check{mem, "SELECT count(*) FROM settings"};
std::cout << check.each().begin()->get<int>(0) << '\n';

Hydrating read-only lookup data? flip read_only = true so SQLite refuses accidental writes against that schema.

API surface

FunctionDescription
constexpr bool serialization_supported() noexceptReturns true when SQLITE_ENABLE_DESERIALIZE is defined.
std::vector<unsigned char> serialize(connection& con, std::string_view schema = "main", unsigned int flags = 0)Dumps an entire schema into a contiguous byte vector.
void deserialize(connection& con, std::span<const unsigned char> image, std::string_view schema = "main", bool read_only = false)Replaces the contents of schema with image; optionally marks it read-only.