60 lines
1.2 KiB
C
Raw Normal View History

2025-03-22 03:15:49 +01:00
#include "db_utils.h"
2025-03-28 06:56:36 +01:00
#include <stdio.h>
2025-03-22 03:15:49 +01:00
void db_initialize() {
2025-03-28 06:56:36 +01:00
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("database.db", &db);
2025-03-22 03:15:49 +01:00
2025-03-28 06:56:36 +01:00
if (rc != SQLITE_OK) {
return;
}
2025-03-22 03:15:49 +01:00
2025-03-28 06:56:36 +01: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 03:15:49 +01:00
2025-03-28 06:56:36 +01:00
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
}
2025-03-22 03:15:49 +01:00
2025-03-28 06:56:36 +01:00
sqlite3_close(db);
2025-03-22 03:15:49 +01:00
}
void test_db_set() {
2025-03-28 06:56:36 +01: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 03:15:49 +01:00
}
void test_db_get() {
2025-03-28 06:56:36 +01: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 03:15:49 +01:00
}
void test_db_query() {
2025-03-28 06:56:36 +01: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 03:15:49 +01:00
}
int main() {
2025-03-28 06:56:36 +01:00
db_initialize();
test_db_set();
test_db_get();
test_db_query();
return 0;
2025-03-22 03:15:49 +01:00
}