Skip to content

Instantly share code, notes, and snippets.

@YellowApple
Created September 28, 2019 02:16
Show Gist options
  • Save YellowApple/1a8fac91369a9ec6c2cd5244d839fad1 to your computer and use it in GitHub Desktop.
Save YellowApple/1a8fac91369a9ec6c2cd5244d839fad1 to your computer and use it in GitHub Desktop.
Opening and closing a SQLite database in Zig
/* Ripped from https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm */
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(":memory:", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
// Direct translation of sqlite-test.c. There seemed to be zero useful search
// results for "zig sqlite", so hopefully this'll fix that :)
//
// Ostensibly can be built with only
// zig build-exe sqlite-test.zig --library sqlite3
// But on my machine (running Slackware64 14.2) I had to be a bit
// more explicit:
// zig build-exe sqlite-test.zig --library sqlite3 --library-path /usr/lib64
use @cImport({
@cInclude("stdio.h");
@cInclude("sqlite3.h");
});
pub fn main() anyerror!void {
var db: ?*sqlite3 = undefined;
var zErrMsg: [*]u8 = undefined;
var rc: c_int = undefined;
rc = sqlite3_open(c":memory:", &db);
if(rc > 0) {
_ = printf(c"Can't open database: %s\n", sqlite3_errmsg(db));
} else {
_ = printf(c"Opened database successfully\n");
}
rc = sqlite3_close(db);
}
@sky5walk
Copy link

Cool, do you mind adding trailing comments to each line?
Ex.

var db: ?*sqlite3 = undefined;       //<-- what do '?' and 'undefined' mean?
var zErrMsg: [*]u8 = undefined;      //<-- [*] is this a [0 byte] char array that dynamically grows?
rc = sqlite3_open(c":memory:", &db); //<-- Why is the 'c' prefix required for the literal ":memory:"?

@YellowApple
Copy link
Author

A lot of this is available in Zig's docs, with examples strewn throughout the standard library, but I don't blame you for not wanting to go spelunking ;) Maybe one of these days I'll turn this into a blog post or something to go through the trial-and-error steps I took to get from working C to working Zig.

To answer your questions:

what do '?' and 'undefined' mean?

? before a type indicates that it's optional, i.e. it can be null. If you were to remove that and attempt to recompile, Zig would yell at you for trying to mix optional and non-optional types.

undefined means we explicitly haven't defined it yet - i.e. we're declaring and setting aside space for a variable on the stack, but we're not initializing the memory yet. It's equivalent to declaring a variable in C without assigning to it (which Zig doesn't allow unless you explicitly assign undefined to it).

[*] is this a [0 byte] char array that dynamically grows?

It's indeed a char/byte array. The [*] means that the length is unknown (e.g. it's a pointer to a C array, which could be either null-terminated or accompanied by some other variable specifying a length; in this case, the former). And on that note...

Why is the 'c' prefix required for the literal ":memory:"?

Because SQLite expects a null-terminated string here. Zig's strings are not null-terminated by default, but if you prefix the string literal with a c, Zig will treat that as a "C-style" null-terminated string instead (i.e. it'll add a 0 byte to the end of the string).

@sky5walk
Copy link

Thanks for the detailed reply.
I do so much spelunking every day, it is nice to have beginner examples self documented.

On the spelunking front, I did not find a dev setup scenario beyond console windows(pashaw).
Can you post what IDE and/or extensions you use?
Say I am Windows centric and have Notepad++ or VS Code or straight up Visual Studio Community Edition.
Will Zig have its own IDE and debugger, or at least a supported extension for a few well known IDE's?

@YellowApple
Copy link
Author

I use Emacs with the zig-mode package. I don't know if there's any work being done on a Zig-focused IDE or on adding IDE-like functionality (like autocompletion or integrated debugging) to any existing IDEs/editors, though it'd be cool if something does pop up. I suspect right now most energy's focused on getting the language stable and usable and well-documented.

@sky5walk
Copy link

haha, I knew it would be Emacs. I guess it's time to give it a go.
A powerful and functional approach to developing a new language is to create an IDE using the same language.
It touches on many programming concepts and serves a purpose in the end.
Still, a community driven extension for VS Code or Emacs is acceptable if the debugger is integrated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment