some db functions

This commit is contained in:
SuperNovaa41 2025-03-03 12:50:43 -05:00
parent 7d8c8c6a80
commit 2052137673
2 changed files with 35 additions and 0 deletions

View File

@ -1,9 +1,22 @@
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include "include/db.h"
static void open_db(sqlite3** db)
{
int err;
err = sqlite3_open("books.db", db);
if (err != SQLITE_OK) {
fprintf(stderr, "Cannot open db: %s\n", sqlite3_errmsg(*db));
sqlite3_close(*db);
exit(EXIT_FAILURE);
}
}
static int get_num_rows(sqlite3* db)
{
int rows = 0;
@ -22,6 +35,8 @@ void setup_db(sqlite3* db)
int err;
char* err_buf = 0;
open_db(&db);
err = sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
title TEXT, subtitle TEXT, isbn TEXT, authors TEXT, subjects TEXT, \
@ -34,4 +49,22 @@ void setup_db(sqlite3* db)
sqlite3_close(db);
exit(EXIT_FAILURE);
}
sqlite3_close(db);
}
#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, date_completed, pub_date, image_code, year_of_pub, page_len) VALUES("
void add_to_db(book_t* book, sqlite3* db)
{
int id;
char* cmd;
open_db(&db);
id = get_num_rows(db) + 1;
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
snprintf(cmd, strlen(INSERT_STR), "%s", INSERT_STR);
}

View File

@ -14,4 +14,6 @@ typedef struct {
void setup_db(sqlite3* db);
void add_to_db(book_t* book, sqlite3* db);
#endif