fixes all db entry issues, db addition now works!

This commit is contained in:
SuperNovaa41 2025-03-05 15:23:12 -05:00
parent a2d4e27d73
commit 3513858264
3 changed files with 80 additions and 25 deletions

View File

@ -1,7 +1,7 @@
CC=gcc CC=gcc
CFLAGS= -c -g -Wall CFLAGS= -c -g -Wall
LDLIBS = -lcurl -lcjson -lsqlite3 LDLIBS = -lcurl -lcjson -lsqlite3 -lm
TARGET := isbn-lookup TARGET := isbn-lookup

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
@ -54,59 +55,91 @@ void setup_db(sqlite3* db)
sqlite3_close(db); sqlite3_close(db);
} }
static void add_str_entry(char** dst, char** src) static void add_str_entry(char** dst, char** src, bool last)
{ {
size_t len; size_t len;
len = strlen(*src) + strlen(*dst) + 1; len = strlen(*src) + strlen(*dst) + 2 + 1 + (last ? 0 : 2);
*dst = malloc(sizeof(char) * len); *dst = realloc(*dst, sizeof(char) * len);
*dst = strcat(*dst, "\"");
*dst = strcat(*dst, *src); *dst = strcat(*dst, *src);
*dst = strcat(*dst, "\"");
if (!last)
*dst = strcat(*dst, ", ");
} }
static void add_int_entry(char** dst, int src) static void add_int_entry(char** dst, int src, bool last)
{ {
size_t len_int, len; size_t len_int, len;
char* num;
if (src == 0) if (src == 0)
len_int = 1; len_int = 1;
else else
len_int = floor(log10(abs(src))) + 1; len_int = floor(log10(abs(src))) + 1;
len = strlen(*dst) + len_int + 1; num = malloc(sizeof(char) * (len_int + 1));
*dst = malloc(sizeof(char) * len); snprintf(num, len_int + 1, "%d", src);
snprintf(*dst, len, "%s%d", *dst, src);
len = strlen(*dst) + len_int + 1 + (last ? 0 : 2);
*dst = realloc(*dst, sizeof(char) * len);
*dst = strcat(*dst, num);
if (!last)
strcat(*dst, ", ");
free(num);
} }
static void string_cat_vals(char** dst, book_t* book, int id) static void string_cat_vals(char** dst, book_t* book, int id)
{ {
add_int_entry(dst, id); add_int_entry(dst, id, false);
add_str_entry(dst, &(book->title)); add_str_entry(dst, &(book->title), false);
add_str_entry(dst, &(book->subtitle)); add_str_entry(dst, &(book->subtitle), false);
add_str_entry(dst, &(book->isbn)); add_str_entry(dst, &(book->isbn), false);
add_str_entry(dst, &(book->authors)); add_str_entry(dst, &(book->authors), false);
add_str_entry(dst, &(book->subjects)); add_str_entry(dst, &(book->subjects), false);
add_str_entry(dst, &(book->langs)); add_str_entry(dst, &(book->langs), false);
add_str_entry(dst, &(book->date_added)); add_str_entry(dst, &(book->date_added), false);
add_str_entry(dst, &(book->pub_date)); add_str_entry(dst, &(book->pub_date), false);
add_str_entry(dst, &(book->image_code)); add_str_entry(dst, &(book->image_code), false);
add_int_entry(dst, book->page_len); add_int_entry(dst, book->page_len, true);
}
*dst = realloc(*dst, sizeof(char) * (strlen(*dst) + 3));
strcat(*dst, ");");
}
#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, pub_date, image_code, page_len) VALUES(" #define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, pub_date, image_code, page_len) VALUES("
void add_to_db(book_t* book, sqlite3* db) void add_to_db(book_t* book, sqlite3* db)
{ {
int id; int id, err;
char* cmd; char* cmd;
char* err_msg = 0;
open_db(&db); open_db(&db);
id = get_num_rows(db) + 1; id = get_num_rows(db) + 4;
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1)); cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
snprintf(cmd, strlen(INSERT_STR), "%s", INSERT_STR); snprintf(cmd, strlen(INSERT_STR) + 1, "%s", INSERT_STR);
string_cat_vals(&cmd, book, id); string_cat_vals(&cmd, book, id);
printf("%s\n", cmd);
err = sqlite3_exec(db, cmd, 0, 0, &err_msg);
if (err != SQLITE_OK) {
fprintf(stderr, "sqlite3_exec: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
exit(EXIT_FAILURE);
}
sqlite3_close(db);
free(cmd);
} }
#undef INSERT_STR

View File

@ -4,6 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "include/curl.h" #include "include/curl.h"
#include "include/db.h"
#include "include/json.h" #include "include/json.h"
struct flags { struct flags {
@ -69,6 +70,7 @@ int main(int argc, char* argv[])
init_flags(&flags); init_flags(&flags);
argp_parse(&argp, argc, argv, 0, 0, &flags); argp_parse(&argp, argc, argv, 0, 0, &flags);
/*
//book_api_call("9780762437818", &resp); //book_api_call("9780762437818", &resp);
book_api_call(flags.isbn, &resp); book_api_call(flags.isbn, &resp);
@ -81,8 +83,28 @@ int main(int argc, char* argv[])
if (flags.json) if (flags.json)
json_out(&query); json_out(&query);
else else
// db out; ; // db out;
*/
book_t book;
book.title = "hi";
book.subtitle = "test";
book.isbn = "1234";
book.authors = "test";
book.subjects = "test";
book.langs = "eng";
book.date_added = "test";
book.pub_date = "test";
book.image_code = "test";
book.page_len = 1;
sqlite3* db;
setup_db(db);
add_to_db(&book, db);
return 0; return 0;