From 3708b21fb020716cbe7e11e8c23a4358b89022a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Stensg=C3=A5rd?= Date: Sun, 6 Jul 2025 17:32:37 +0200 Subject: select_booking: SELECT from event --- .gitignore | 1 + Makefile | 5 ++++- select_booking.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 select_booking.c diff --git a/.gitignore b/.gitignore index d1ea0e8..c3426ad 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ /check_ical_out.txt /events.ical /opplysning +/select_booking /sqlite3 core diff --git a/Makefile b/Makefile index d260340..83908ff 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LIBS = -lraylib -lm -lpthread -lGLESv2 -lEGL \ -lical -licalss -licalvcal \ $$([ `uname -m` = aarch64 ] && echo '-lvcos -lvchiq_arm -lgbm -ldrm') -all: check_ical import_booking opplysning +all: check_ical import_booking opplysning select_booking check: all ./check_ical < check_ical_in.ical > check_ical_out.txt @@ -29,6 +29,9 @@ import_booking: import_booking.c sqlite.o opplysning: opplysning.c $(CC) $(CFLAGS) -o $@ opplysning.c $(LIBS) +select_booking: select_booking.c sqlite.o + $(CC) $(CFLAGS) -o $@ select_booking.c sqlite.o + sqlite.o: sqlite/sqlite3.c $(CC) $(CFLAGS) -c -o $@ sqlite/sqlite3.c diff --git a/select_booking.c b/select_booking.c new file mode 100644 index 0000000..4eaadcb --- /dev/null +++ b/select_booking.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#include "sqlite/sqlite3.h" + +static const char *DATABASE = "booking.db"; + +int +main(void) +{ + sqlite3 *db; + if (sqlite3_open(DATABASE, &db)) + errx(1, "sqlite3_open: %s", sqlite3_errmsg(db)); + + const char *sql = "SELECT " + "datetime(start, 'unixepoch'), " + "datetime(end, 'unixepoch'), " + "(end-start)/60/60, " + "summary " + "FROM event " + "WHERE unixepoch()-6*3600 < start " + "AND summary IS NOT NULL " + "ORDER BY start"; + sqlite3_stmt *stmt = NULL; + if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL)) + errx(1, "sqlite3_prepare_v2: %s", sqlite3_errmsg(db)); + + for (;;) { + int step = sqlite3_step(stmt); + if (SQLITE_DONE == step) + break; + if (SQLITE_ROW != step) + errx(1, "sqlite3_step: %s", sqlite3_errmsg(db)); + + const unsigned char *summary = sqlite3_column_text(stmt, 3); + assert(summary); + printf("%s\n", summary); + } + + sqlite3_close(db); +} -- cgit v1.2.3