Created
July 27, 2022 19:37
-
-
Save billywhizz/c1e68fd8d9b3fc4f50df49eb0903f32e to your computer and use it in GitHub Desktop.
Statically Linked duckdb demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
curl -L -o libduckdb.zip https://github.com/duckdb/duckdb/releases/download/v0.3.2/libduckdb-src.zip | |
unzip libduckdb.zip | |
g++ -fPIC -c -o duckdb.o duckdb.cpp | |
gcc -c -o main.o main.c | |
gcc -static -flto -pthread main.o duckdb.o -o test -ldl -lrt -lm -lstdc++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "duckdb.h" | |
int main (int argc, char** argv) { | |
duckdb_database db; | |
duckdb_config config; | |
const char* filename = "quack.duck"; | |
duckdb_connection con; | |
duckdb_result result; | |
duckdb_state rc; | |
rc = duckdb_create_config(&config); | |
if (rc == DuckDBError) { | |
perror("cannot create config"); | |
exit(1); | |
} | |
rc = duckdb_set_config(config, "access_mode", "READ_WRITE"); | |
if (rc == DuckDBError) { | |
perror("cannot set config access mode"); | |
exit(1); | |
} | |
rc = duckdb_set_config(config, "threads", "1"); | |
if (rc == DuckDBError) { | |
perror("cannot set config threads"); | |
exit(1); | |
} | |
rc = duckdb_set_config(config, "max_memory", "1GB"); | |
if (rc == DuckDBError) { | |
perror("cannot set config max memory"); | |
exit(1); | |
} | |
rc = duckdb_set_config(config, "default_order", "DESC"); | |
if (rc == DuckDBError) { | |
perror("cannot set config order"); | |
exit(1); | |
} | |
rc = duckdb_open_ext(filename, &db, config, NULL); | |
if (rc == DuckDBError) { | |
perror("cannot open database"); | |
exit(1); | |
} | |
rc = duckdb_connect(db, &con); | |
if (rc == DuckDBError) { | |
perror("cannot connect to database"); | |
exit(1); | |
} | |
rc = duckdb_query(con, "CREATE TABLE IF NOT EXISTS integers(i INTEGER, j INTEGER);", &result); | |
if (rc == DuckDBError) { | |
perror("cannot create table"); | |
exit(1); | |
} | |
rc = duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", &result); | |
if (rc == DuckDBError) { | |
perror("cannot insert values"); | |
exit(1); | |
} | |
rc = duckdb_query(con, "SELECT * FROM integers;", &result); | |
if (rc == DuckDBError) { | |
perror("cannot select values"); | |
exit(1); | |
} | |
int rows = duckdb_row_count(&result); | |
int cols = duckdb_column_count(&result); | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
char* v = duckdb_value_varchar(&result, j, i); | |
fprintf(stdout, "row %i col %i value %s\n", i, j, v); | |
} | |
} | |
duckdb_destroy_result(&result); | |
duckdb_disconnect(&con); | |
duckdb_close(&db); | |
duckdb_destroy_config(&config); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment