#include"dbx.h"
#include<stdio.h>
#include<time.h>typedefstruct{DbxDatabase*db;}LogCollector;LogCollector*log_collector_create(constchar*db_path){LogCollector*collector=malloc(sizeof(LogCollector));collector->db=dbx_open(db_path);dbx_execute_sql(collector->db,"CREATE TABLE IF NOT EXISTS logs ("" timestamp INTEGER,"" level TEXT,"" message TEXT"")");returncollector;}voidlog_collector_add(LogCollector*collector,constchar*level,constchar*message){charsql[1024];snprintf(sql,sizeof(sql),"INSERT INTO logs VALUES (%ld, '%s', '%s')",time(NULL),level,message);dbx_execute_sql(collector->db,sql);}voidlog_collector_destroy(LogCollector*collector){dbx_close(collector->db);free(collector);}
Game Save System (C++)
#include"dbx.hpp"
#include<nlohmann/json.hpp>classGameSaveSystem{private:dbx::Databasedb;public:GameSaveSystem(conststd::string&dbPath):db(dbx::Database::open(dbPath)){db.executeSql(R"(
CREATE TABLE IF NOT EXISTS saves (
slot INTEGER PRIMARY KEY,
player_name TEXT,
level INTEGER,
score INTEGER,
data TEXT
)
)");}voidsaveGame(intslot,conststd::string&playerName,intlevel,intscore,constjson&gameData){std::ostringstreamsql;sql<<"INSERT OR REPLACE INTO saves VALUES ("<<slot<<", '"<<playerName<<"', "<<level<<", "<<score<<", '"<<gameData.dump()<<"')";db.executeSql(sql.str());}std::optional<json>loadGame(intslot){autoresult=db.executeSql("SELECT data FROM saves WHERE slot = "+std::to_string(slot));if(result.empty())returnstd::nullopt;returnjson::parse(result);}};
Embedded System (C)
typedefstruct{intsensor_id;floattemperature;floathumidity;longtimestamp;}SensorData;voidstore_sensor_data(DbxDatabase*db,constSensorData*data){charsql[256];snprintf(sql,sizeof(sql),"INSERT INTO sensor_data VALUES (%d, %.2f, %.2f, %ld)",data->sensor_id,data->temperature,data->humidity,data->timestamp);dbx_execute_sql(db,sql);}