64 lines
1.4 KiB
C
Raw Normal View History

2025-03-22 02:15:49 +00:00
#include "db_utils.h"
2025-03-28 05:56:36 +00:00
#include <stdio.h>
2025-04-03 03:26:11 +00:00
#include <stdlib.h>
#include <sqlite3.h>
2025-03-22 02:15:49 +00:00
void db_initialize() {
2025-03-28 05:56:36 +00:00
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("database.db", &db);
2025-03-22 02:15:49 +00:00
2025-03-28 05:56:36 +00:00
if (rc != SQLITE_OK) {
2025-04-03 03:26:11 +00:00
fprintf(stderr, "Error: Cannot open database: %s\n", sqlite3_errmsg(db));
2025-03-28 05:56:36 +00:00
return;
}
2025-03-22 02:15:49 +00:00
2025-03-28 05:56:36 +00:00
const char *sql =
"CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
2025-03-22 02:15:49 +00:00
2025-03-28 05:56:36 +00:00
if (rc != SQLITE_OK) {
2025-04-03 03:26:11 +00:00
fprintf(stderr, "SQL error: %s\n", err_msg);
2025-03-28 05:56:36 +00:00
sqlite3_free(err_msg);
}
2025-03-22 02:15:49 +00:00
2025-03-28 05:56:36 +00:00
sqlite3_close(db);
2025-03-22 02:15:49 +00:00
}
void test_db_set() {
2025-03-28 05:56:36 +00:00
json_object *result = db_set("test_key", "test_value");
if (result) {
printf("db_set: %s\n", json_object_get_string(result));
json_object_put(result);
} else {
printf("db_set failed\n");
}
2025-03-22 02:15:49 +00:00
}
void test_db_get() {
2025-03-28 05:56:36 +00:00
json_object *result = db_get("test_key");
if (result) {
printf("db_get: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_get failed\n");
}
2025-03-22 02:15:49 +00:00
}
void test_db_query() {
2025-03-28 05:56:36 +00:00
json_object *result = db_query("SELECT * FROM kv_store");
if (result) {
printf("db_query: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_query failed\n");
}
2025-03-22 02:15:49 +00:00
}
int main() {
2025-03-28 05:56:36 +00:00
db_initialize();
test_db_set();
test_db_get();
test_db_query();
return 0;
2025-03-22 02:15:49 +00:00
}