src/db.c aktualisiert
parent
2751c5ca7b
commit
385383d03e
57
src/db.c
57
src/db.c
|
@ -9,5 +9,60 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "planner.h"
|
/************************************************************
|
||||||
|
*Create a database and handel it
|
||||||
|
*Created by Jan on 13.12.2024.
|
||||||
|
*
|
||||||
|
************************************************************/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "sqlite3.h"
|
||||||
|
#include "planner.h"
|
||||||
|
|
||||||
|
extern struct llist;
|
||||||
|
|
||||||
|
// Initialize a new Database if there no one.
|
||||||
|
void init_db(sqlite3 *db) {
|
||||||
|
const char *sql_LinkedList =
|
||||||
|
"CREATE TABLE LinkedList ("
|
||||||
|
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||||
|
"data TEXT;)";
|
||||||
|
|
||||||
|
// Checks the SQL answer and if it's not ok prints the error code
|
||||||
|
char *errmsg = NULL;
|
||||||
|
if (sqlite3_exec(db, sql_LinkedList, NULL, NULL, &errmsg) != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "Error in creation Table: %s\n", errmsg);
|
||||||
|
sqlite3_free(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save a single node of the linkedlist in the database
|
||||||
|
// node needs to be replaced with the correct name and pointer
|
||||||
|
void save_node_to_db(sqlite3 *db, Node *node) {
|
||||||
|
|
||||||
|
char* errmsg = NULL;
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
const char* sql = "INSERT INTO LinkedList (id, data) VALUES (?, ?);";
|
||||||
|
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||||
|
} else {
|
||||||
|
sqlite3_bind_int(stmt, 1, node->id); // ? -> node->id
|
||||||
|
sqlite3_bind_text(stmt, 2, node->data, -1, SQLITE_STATIC); // ? -> node->data
|
||||||
|
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
||||||
|
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||||
|
} else {
|
||||||
|
printf("Node with ID %d saved.\n", node->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save a whole linkedlist in the database
|
||||||
|
// node needs to be replaced with the correct name and pointer
|
||||||
|
void save_linked_list_to_db(sqlite3* db, Node* head) {
|
||||||
|
Node* current = head;
|
||||||
|
while (current != NULL) {
|
||||||
|
save_node_to_db(db, current);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue