From 848c5cca715c514ce6e57e0fa7ef8e71b09c0b08 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Mon, 25 May 2026 23:00:48 +0200 Subject: Updates sql_exec function to allow reading data --- core/sql.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'core/sql.c') diff --git a/core/sql.c b/core/sql.c index 4cfa1b0..b1c0c2a 100644 --- a/core/sql.c +++ b/core/sql.c @@ -2,7 +2,7 @@ sqlite3 *g_sim_db; -void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, const char *sql_format, ...) { +void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, void (*callback)(void *data), void *data, const char *sql_format, ...) { assert(sql_format); va_list args; @@ -29,18 +29,26 @@ void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, const cha assert(sql_res == SQLITE_OK); } - // Only handle SQLITE_BUSY error, in which case we retry the query. - // Setting 'journal_mode=wal;' should help prevent busy database errors. while (true) { sql_res = sqlite3_step(sql_stmt); - if (sql_res == SQLITE_DONE || sql_res == SQLITE_ROW) { + if (sql_res == SQLITE_ROW) { + if (callback) { + callback(data); + } + + continue; + } + + if (sql_res == SQLITE_DONE) { break; } log_warn("SQLite database returned error %d with message:", sql_res); log_warn(sqlite3_errmsg(g_sim_db)); + // Only handle SQLITE_BUSY error, in which case we retry the query. + // Setting 'journal_mode=wal;' should help prevent busy database errors. if (sql_res == SQLITE_BUSY) { log_info("Will retry query..."); continue; @@ -62,7 +70,7 @@ void sql_open(void) { // Enable Write-Ahead Logging (WAL) // This seems to help prevent DB locks when displaying live data. // See: https://sqlite.org/wal.html - sql_exec(0, NULL, NULL, "pragma journal_mode=wal;"); + sql_exec(0, NULL, NULL, NULL, NULL, "pragma journal_mode=wal;"); } void sql_close(void) { -- cgit v1.3