Read query results in one go
masterYou can fetch query results directly into Zig types using several methods on sqlite.Statement. These methods take a type as the first parameter representing a 'row'.
A row type can be:
- A
structwhere fields map to columns (field 0 maps to column 1, etc.). - A single type (e.g.,
usize,[]const u8) if the resultset has exactly one column.
Available methods:
Statement.one(Type, params, ...): Does not allocate memory (except what SQLite allocates). Returns an optional?Type.Statement.all(Type, allocator, ...): Allocates an array ofTypeusing the provided allocator.Statement.oneAlloc(Type, allocator, ...): Allocates a singleTypeusing the provided allocator.
Note: When reading TEXT into an array, you must use a sentinel-terminated array (e.g., [128:0]u8) to handle variable lengths.
// Using Statement.one with a struct
const row = try stmt.one(
struct {
name: [128:0]u8,
age: usize,
},
.{},
.{ .id = 20 },
);
// Using Statement.all with an allocator
const names = try stmt.all([]const u8, allocator, .{}, .{ .age1 = 20, .age2 = 40 });
// Using Statement.oneAlloc with an allocator
const row = try stmt.oneAlloc([]const u8, allocator, .{}, .{ .id = 200 });