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

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