{"id":5167,"date":"2026-04-07T19:38:29","date_gmt":"2026-04-07T10:38:29","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5167"},"modified":"2026-04-07T19:52:40","modified_gmt":"2026-04-07T10:52:40","slug":"webserver-httplib-sqlite-macos","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/04\/07\/webserver-httplib-sqlite-macos\/","title":{"rendered":"[Webserver]httplib webserver+sqlite(MacOS)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb C++\uc758 httplib\ub97c \uc0ac\uc6a9\ud55c \ub9e4\uc6b0 \uac04\ub2e8\ud55c \uc6f9\uc11c\ubc84 \uc785\ub2c8\ub2e4.<br>This is a very simple web server using C++&#8217;s httplib.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb sqllite\ub370\uc774\ud130\ubca0\uc774\uc2a4\ub97c \ucd94\uac00\ud588\uc2b5\ub2c8\ub2e4.<br>Added SQLite database.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ub370\uc774\ud130 \ucd94\uac00 \uc0ad\uc81c \ubc0f \uc804\uccb4\ub370\uc774\ud130 \ubcf4\uae30\ub97c \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>You can add, delete, and view all data.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\ucf54\ub4dc \/ code<\/p>\n\n\n\n<p>\u2714\ufe0f httplib.h<\/p>\n\n\n\n<p>&#8212; \uc544\ub798\uc758 \uba85\ub839\uc5b4\ub85c \ub2e4\uc6b4\ub85c\ub4dc\ud569\ub2c8\ub2e4.<br>Download using the command below.<\/p>\n\n\n\n<p>\u2714\ufe0f server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"httplib.h\"\n#include &lt;sqlite3.h&gt;\n#include &lt;iostream&gt;\n\nint main( ) {\n    httplib::Server svr;\n    sqlite3* db;\n\n    \/\/ \ub370\uc774\ud130\ubca0\uc774\uc2a4 \ud30c\uc77c \uc5f4\uae30 (\uc5c6\uc73c\uba74 \uc0dd\uc131\ub428 )\n    \/\/ Open database file (creates if it does not exist)\n    if (sqlite3_open(\"test.db\", &amp;db)) {\n        std::cerr &lt;&lt; \"DB open failed!\" &lt;&lt; std::endl;\n        return 1;\n    }\n    \/\/ \ub8e8\ud2b8 \ub77c\uc6b0\ud130(html\uc801\uc6a9)\n    \/\/ Root Router (HTML applied)\n    svr.Get(\"\/\",&#91;](const httplib::Request &amp;req, httplib::Response &amp;res){\n     res.set_content(\"&lt;h1&gt;Hello! silver hand!!!\ud83d\udc4b&lt;\/h1&gt;\",\"text\/html; charset=utf-8\");\n    });\n\n    \/\/ \ud14c\uc774\ube14 \uc0dd\uc131 \ubc0f \ucd08\uae30 \ub370\uc774\ud130 \uc785\ub825\n    \/\/ Create table and input initial data\n    svr.Get(\"\/init\", &#91;&amp;](const httplib::Request&amp;, httplib::Response&amp; res ) {\n        char* errMsg = nullptr;\n\n        \/\/ \ud14c\uc774\ube14 \uc0dd\uc131 SQL\n        \/\/ table creation SQL\n        const char* create_sql = \"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);\";\n        if (sqlite3_exec(db, create_sql, nullptr, nullptr, &amp;errMsg) != SQLITE_OK) {\n            res.set_content(\"{\\\"error\\\": \\\"Table creation failed: \" + std::string(errMsg) + \"\\\"}\", \"application\/json\");\n            sqlite3_free(errMsg);\n            return;\n        }\n\n        \/\/ \ud14c\uc2a4\ud2b8 \ub370\uc774\ud130 \uc0bd\uc785 (Silver Hand \ucd94\uac00)\n        \/\/ Insert test data (Add Silver Hand)\n        const char* insert_sql = \"INSERT INTO users (name) VALUES ('Silver Hand \ud83d\udc4b');\";\n        if (sqlite3_exec(db, insert_sql, nullptr, nullptr, &amp;errMsg) != SQLITE_OK) {\n            res.set_content(\"{\\\"error\\\": \\\"Insert failed: \" + std::string(errMsg) + \"\\\"}\", \"application\/json\");\n            sqlite3_free(errMsg);\n            return;\n        }\n\n        res.set_content(\"{\\\"message\\\": \\\"Table created and data initialized!\\\"}\", \"application\/json; charset=utf-8\");\n    });\n\n\n    \/\/ API \uc694\uccad \uc2dc DB \uc870\ud68c\ud558\uae30\n    \/\/ Querying DB when making API requests\n    svr.Get(\"\/user\", &#91;&amp;](const httplib::Request&amp;, httplib::Response&amp; res ) {\n        \/\/ \uac00\uc7a5 \ucd5c\uadfc\uc5d0 \ucd94\uac00\ub41c \uc0ac\uc6a9\uc790 1\uba85\uc758 \uc774\ub984\uc744 \uac00\uc838\uc635\ub2c8\ub2e4.\n        const char* sql = \"SELECT name FROM users ORDER BY id DESC LIMIT 1;\";\n        sqlite3_stmt* stmt;\n\n        if (sqlite3_prepare_v2(db, sql, -1, &amp;stmt, nullptr) == SQLITE_OK) {\n            if (sqlite3_step(stmt) == SQLITE_ROW) {\n                const char* name = (const char*)sqlite3_column_text(stmt, 0);\n                res.set_content(\"{\\\"username\\\": \\\"\" + std::string(name ? name : \"\") + \"\\\"}\", \"application\/json; charset=utf-8\");\n            } else {\n                res.status = 404;\n                res.set_content(\"{\\\"error\\\": \\\"No users found. Please run \/init first.\\\"}\", \"application\/json; charset=utf-8\");\n            }\n            sqlite3_finalize(stmt);\n        } else {\n            res.set_content(\"{\\\"error\\\": \\\"Query failed\\\"}\", \"application\/json\");\n        }\n    });\n\n\n    \/\/ \uc804\uccb4 \uc0ac\uc6a9\uc790 \ubaa9\ub85d \uc870\ud68c\n    \/\/ View all user list\n    svr.Get(\"\/users\", &#91;&amp;](const httplib::Request&amp;, httplib::Response&amp; res ) {\n        const char* sql = \"SELECT id, name FROM users;\";\n        sqlite3_stmt* stmt;\n        std::string json = \"&#91;\";\n\n        if (sqlite3_prepare_v2(db, sql, -1, &amp;stmt, nullptr) == SQLITE_OK) {\n            bool first = true;\n            while (sqlite3_step(stmt) == SQLITE_ROW) {\n                if (!first) json += \",\";\n                int id = sqlite3_column_int(stmt, 0);\n                const char* name = (const char*)sqlite3_column_text(stmt, 1);\n                json += \"{\\\"id\\\":\" + std::to_string(id) + \", \\\"name\\\":\\\"\" + (name ? name : \"\") + \"\\\"}\";\n                first = false;\n            }\n            sqlite3_finalize(stmt);\n        }\n        json += \"]\";\n        res.set_content(json, \"application\/json; charset=utf-8\");\n    });\n\n    \/\/ \uc804\uccb4 \ub370\uc774\ud130 \uc0ad\uc81c\n    \/\/ Delete all data\n    svr.Get(\"\/clear\", &#91;&amp;](const httplib::Request&amp;, httplib::Response&amp; res ) {\n        char* errMsg = nullptr;\n        \/\/const char* delete_sql = \"DELETE FROM users;\";\n        \/\/\ub370\uc774\ud130 \uc0ad\uc81c + ID \uce74\uc6b4\ud130 \ucd08\uae30\ud654 \/ Delete all data and reset ID counter\n        const char* delete_sql = \"DELETE FROM users; DELETE FROM sqlite_sequence WHERE name='users';\";\n\n\n        if (sqlite3_exec(db, delete_sql, nullptr, nullptr, &amp;errMsg) != SQLITE_OK) {\n            res.set_content(\"{\\\"error\\\": \\\"Delete failed: \" + std::string(errMsg) + \"\\\"}\", \"application\/json\");\n            sqlite3_free(errMsg);\n            return;\n        }\n\n        res.set_content(\"{\\\"message\\\": \\\"All data cleared!\\\"}\", \"application\/json; charset=utf-8\");\n    });\n\n\n    std::cout &lt;&lt; \"server listen 0.0.0.0:8080\" &lt;&lt; std::endl;\n    svr.listen(\"0.0.0.0\", 8080);\n\n    sqlite3_close(db);\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ -o server server.cpp -std=c++11 -pthread -lsqlite3<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc11c\ubc84\uc2e4\ud589 \/ Run server<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"124\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-1024x124.jpg\" alt=\"\" class=\"wp-image-5170\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-1024x124.jpg 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-300x36.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-768x93.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-400x48.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart-800x97.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/serverstart.jpg 1124w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">.\/server<\/figcaption><\/figure>\n\n\n\n<p>\u2714\ufe0f \ube0c\ub77c\uc6b0\uc800\uc5d0\uc11c \ub77c\uc6b0\ud2b8 \uc2e4\ud589 \/ Run route in browser<\/p>\n\n\n\n<p>&#8212; \ub8e8\ud2b8 \ub77c\uc6b0\ud2b8 \/ root route<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"454\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/root.jpg\" alt=\"\" class=\"wp-image-5168\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/root.jpg 730w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/root-300x187.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/root-400x249.jpg 400w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption class=\"wp-element-caption\">\/<\/figcaption><\/figure>\n\n\n\n<p>&#8212; \ub370\uc774\ud130\uc785\ub825 \ub77c\uc6b0\ud2b8 \/ Data input route<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"386\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init.jpg\" alt=\"\" class=\"wp-image-5174\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init.jpg 868w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init-300x133.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init-768x342.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init-400x178.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/init-800x356.jpg 800w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><figcaption class=\"wp-element-caption\">\/init<\/figcaption><\/figure>\n\n\n\n<p>&#8212; \ub370\uc774\ud130 \uc785\ub825\ud655\uc778 \ub77c\uc6b0\ud2b8 \/ Data input verification route<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"886\" height=\"434\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user.jpg\" alt=\"\" class=\"wp-image-5177\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user.jpg 886w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user-300x147.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user-768x376.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user-400x196.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/user-800x392.jpg 800w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><figcaption class=\"wp-element-caption\">\/user<\/figcaption><\/figure>\n\n\n\n<p>&#8212; \uc804\uccb4 \ub370\uc774\ud130 \ud655\uc778 \/ Check full data<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"376\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/users.jpg\" alt=\"\" class=\"wp-image-5179\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/users.jpg 750w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/users-300x150.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/users-400x201.jpg 400w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption class=\"wp-element-caption\">\/users<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb C++\uc758 httplib\ub97c \uc0ac\uc6a9\ud55c \ub9e4\uc6b0 \uac04\ub2e8\ud55c \uc6f9\uc11c\ubc84 \uc785\ub2c8\ub2e4.This is a very simple web server using C++&#8217;s httplib. \ud83d\udc49\ud83c\udffb sqllite\ub370\uc774\ud130\ubca0\uc774\uc2a4\ub97c \ucd94\uac00\ud588\uc2b5\ub2c8\ub2e4.Added SQLite database. \ud83d\udc49\ud83c\udffb \ub370\uc774\ud130 \ucd94\uac00 \uc0ad\uc81c \ubc0f \uc804\uccb4\ub370\uc774\ud130 \ubcf4\uae30\ub97c \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.You can add, delete, and view all data. \ud83d\udc49\ud83c\udffb\ucf54\ub4dc \/ code \u2714\ufe0f httplib.h &#8212; \uc544\ub798\uc758 \uba85\ub839\uc5b4\ub85c \ub2e4\uc6b4\ub85c\ub4dc\ud569\ub2c8\ub2e4.Download using the command below. \u2714\ufe0f server.cpp \ud83d\udc49\ud83c\udffb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5167","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5167"}],"version-history":[{"count":11,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5167\/revisions"}],"predecessor-version":[{"id":5184,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5167\/revisions\/5184"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}