#include"dbx.hpp"
#include"httplib.h"
#include<nlohmann/json.hpp>usingjson=nlohmann::json;intmain(){autodb=dbx::Database::open("api.db");db.executeSql("CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT, email TEXT)");httplib::Serversvr;svr.Post("/users",[&](consthttplib::Request&req,httplib::Response&res){autobody=json::parse(req.body);intid=std::time(nullptr);std::ostringstreamsql;sql<<"INSERT INTO users VALUES ("<<id<<", '"<<body["name"].get<std::string>()<<"', '"<<body["email"].get<std::string>()<<"')";db.executeSql(sql.str());jsonresponse={{"id",id},{"name",body["name"]},{"email",body["email"]}};res.set_content(response.dump(),"application/json");});svr.Get(R"(/users/(\d+))",[&](consthttplib::Request&req,httplib::Response&res){intid=std::stoi(req.matches[1]);autoresult=db.executeSql("SELECT * FROM users WHERE id = "+std::to_string(id));res.set_content(result,"application/json");});svr.listen("0.0.0.0",8080);return0;}
로그 수집기 (C)
#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];time_tnow=time(NULL);snprintf(sql,sizeof(sql),"INSERT INTO logs VALUES (%ld, '%s', '%s')",now,level,message);dbx_execute_sql(collector->db,sql);}char*log_collector_get_errors(LogCollector*collector){returndbx_execute_sql(collector->db,"SELECT * FROM logs WHERE level = 'ERROR'");}voidlog_collector_destroy(LogCollector*collector){dbx_close(collector->db);free(collector);}intmain(){LogCollector*collector=log_collector_create("logs.db");log_collector_add(collector,"INFO","Application started");log_collector_add(collector,"ERROR","Connection failed");char*errors=log_collector_get_errors(collector);printf("Errors: %s\n",errors);dbx_free_string(errors);log_collector_destroy(collector);return0;}
임베디드 시스템 (C)
#include"dbx.h"
#include<stdio.h>// 센서 데이터 저장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);}intmain(){DbxDatabase*db=dbx_open("sensors.db");dbx_execute_sql(db,"CREATE TABLE IF NOT EXISTS sensor_data ("" sensor_id INTEGER,"" temperature REAL,"" humidity REAL,"" timestamp INTEGER"")");// 센서 데이터 수집for(inti=0;i<100;i++){SensorDatadata={.sensor_id=1,.temperature=20.0+(i%10),.humidity=50.0+(i%20),.timestamp=time(NULL)};store_sensor_data(db,&data);}// 통계 조회char*stats=dbx_execute_sql(db,"SELECT AVG(temperature), AVG(humidity) FROM sensor_data");printf("Stats: %s\n",stats);dbx_free_string(stats);dbx_close(db);return0;}
게임 저장 시스템 (C++)
#include"dbx.hpp"
#include<nlohmann/json.hpp>usingjson=nlohmann::json;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,
timestamp INTEGER
)
)");}voidsaveGame(intslot,conststd::string&playerName,intlevel,intscore,constjson&gameData){autonow=std::time(nullptr);std::ostringstreamsql;sql<<"INSERT OR REPLACE INTO saves VALUES ("<<slot<<", '"<<playerName<<"', "<<level<<", "<<score<<", '"<<gameData.dump()<<"', "<<now<<")";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);}voiddeleteGame(intslot){db.executeSql("DELETE FROM saves WHERE slot = "+std::to_string(slot));}};intmain(){GameSaveSystemsaves("game.db");jsongameData={{"position",{{"x",100},{"y",200}}},{"inventory",{"sword","shield","potion"}},{"quests",{{"main","completed"},{"side","in_progress"}}}};saves.saveGame(1,"Player1",10,5000,gameData);autoloaded=saves.loadGame(1);if(loaded){std::cout<<"Loaded game: "<<loaded->dump(2)<<std::endl;}return0;}