fixes all db entry issues, db addition now works!
This commit is contained in:
parent
a2d4e27d73
commit
3513858264
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
CC=gcc
|
||||
CFLAGS= -c -g -Wall
|
||||
|
||||
LDLIBS = -lcurl -lcjson -lsqlite3
|
||||
LDLIBS = -lcurl -lcjson -lsqlite3 -lm
|
||||
|
||||
TARGET := isbn-lookup
|
||||
|
||||
|
79
src/db.c
79
src/db.c
@ -1,5 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <sqlite3.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
@ -54,59 +55,91 @@ void setup_db(sqlite3* 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;
|
||||
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, "\"");
|
||||
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;
|
||||
char* num;
|
||||
|
||||
if (src == 0)
|
||||
len_int = 1;
|
||||
else
|
||||
len_int = floor(log10(abs(src))) + 1;
|
||||
|
||||
len = strlen(*dst) + len_int + 1;
|
||||
*dst = malloc(sizeof(char) * len);
|
||||
snprintf(*dst, len, "%s%d", *dst, src);
|
||||
num = malloc(sizeof(char) * (len_int + 1));
|
||||
snprintf(num, len_int + 1, "%d", 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)
|
||||
{
|
||||
add_int_entry(dst, id);
|
||||
add_str_entry(dst, &(book->title));
|
||||
add_str_entry(dst, &(book->subtitle));
|
||||
add_str_entry(dst, &(book->isbn));
|
||||
add_str_entry(dst, &(book->authors));
|
||||
add_str_entry(dst, &(book->subjects));
|
||||
add_str_entry(dst, &(book->langs));
|
||||
add_str_entry(dst, &(book->date_added));
|
||||
add_str_entry(dst, &(book->pub_date));
|
||||
add_str_entry(dst, &(book->image_code));
|
||||
add_int_entry(dst, book->page_len);
|
||||
}
|
||||
add_int_entry(dst, id, false);
|
||||
add_str_entry(dst, &(book->title), false);
|
||||
add_str_entry(dst, &(book->subtitle), false);
|
||||
add_str_entry(dst, &(book->isbn), false);
|
||||
add_str_entry(dst, &(book->authors), false);
|
||||
add_str_entry(dst, &(book->subjects), false);
|
||||
add_str_entry(dst, &(book->langs), false);
|
||||
add_str_entry(dst, &(book->date_added), false);
|
||||
add_str_entry(dst, &(book->pub_date), false);
|
||||
add_str_entry(dst, &(book->image_code), false);
|
||||
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("
|
||||
|
||||
void add_to_db(book_t* book, sqlite3* db)
|
||||
{
|
||||
int id;
|
||||
int id, err;
|
||||
char* cmd;
|
||||
char* err_msg = 0;
|
||||
|
||||
open_db(&db);
|
||||
|
||||
id = get_num_rows(db) + 1;
|
||||
id = get_num_rows(db) + 4;
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
|
24
src/main.c
24
src/main.c
@ -4,6 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "include/curl.h"
|
||||
#include "include/db.h"
|
||||
#include "include/json.h"
|
||||
|
||||
struct flags {
|
||||
@ -69,6 +70,7 @@ int main(int argc, char* argv[])
|
||||
init_flags(&flags);
|
||||
argp_parse(&argp, argc, argv, 0, 0, &flags);
|
||||
|
||||
/*
|
||||
//book_api_call("9780762437818", &resp);
|
||||
book_api_call(flags.isbn, &resp);
|
||||
|
||||
@ -81,8 +83,28 @@ int main(int argc, char* argv[])
|
||||
if (flags.json)
|
||||
json_out(&query);
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user