vrischmann / zig-sqlite

zig-sqlite is a small wrapper around sqlite's C API, making it easier to use with Zig.
MIT License
367 stars 49 forks source link

"PRAGMA journal_mode = WAL" gives "panic: invalid result code 100" #160

Closed robguthrie closed 4 months ago

robguthrie commented 4 months ago

zig-sqlite commit

fd17eb9a4ea35f515cc7c2e4755a3a5e284a0bd3

Zig version

0.13.0-dev.211+6a65561e3

Steps to reproduce

const std = @import("std");
const sqlite = @import("sqlite");

pub fn main() !void {
var db = try sqlite.Db.init(.{
        .mode = sqlite.Db.Mode{ .File = "test.db"},
        .open_flags = .{
            .write = true,
            .create = true,
        },
        .threading_mode = .Serialized,
});
try db.exec("PRAGMA journal_mode = WAL", .{}, .{});
}

or something similar to that... gives something similar to the following error

thread 4191359 panic: invalid result code 100
/Users/rob/projects/pacz/lib/zig-sqlite/errors.zig:269:32: 0x1059575c0 in errorFromResultCode (pacz)
        else => std.debug.panic("invalid result code {}", .{code}),
                               ^
/Users/rob/projects/pacz/lib/zig-sqlite/sqlite.zig:2140:54: 0x105959065 in exec__anon_4777 (pacz)
                    return errors.errorFromResultCode(result);
                                                     ^
/Users/rob/projects/pacz/lib/zig-sqlite/sqlite.zig:476:22: 0x105959145 in exec__anon_4753 (pacz)
        try stmt.exec(options, values);
                     ^
/Users/rob/projects/pacz/src/db.zig:35:16: 0x10595cafd in createTables (pacz)
    try db.exec("PRAGMA journal_mode = WAL", .{}, .{});

The short version of this, is that running some PRAGMA expressions fails with a 100 result code.

https://www.sqlite.org/rescode.html says:

(100) SQLITE_ROW

The SQLITE_ROW result code returned by sqlite3_step() indicates that another row of output is available.

I think this is because, when in the cli, the command returns "wal" or "memory". Which can safely be ignored.

Anyway.. I'll just conclude this issue by saying I'm REALLY enjoying your library. Thank you so much, it's been a joy to learn zig with this at the center of my app.

Expected behaviour

just work/return normally. do I have to keep writing in here to get the green button to show up? oh there it goes

robguthrie commented 4 months ago

Oh, I just found you have a pragma function for this stuff.

solved with

    if (try db.pragmaAlloc([]const u8, allocator, .{}, "journal_mode", "WAL")) |journal_mode| {
        allocator.free(journal_mode);
    }