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

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