more db stuff!
This commit is contained in:
parent
2052137673
commit
a2d4e27d73
46
src/db.c
46
src/db.c
@ -2,6 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <sqlite3.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "include/db.h"
|
||||
|
||||
@ -40,7 +41,7 @@ void setup_db(sqlite3* 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, \
|
||||
langs TEXT, date_added TEXT, date_completed TEXT, pub_date TEXT, \
|
||||
langs TEXT, date_added TEXT, pub_date TEXT, \
|
||||
image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
|
||||
0, 0, &err_buf);
|
||||
if (err != SQLITE_OK) {
|
||||
@ -53,7 +54,47 @@ void setup_db(sqlite3* db)
|
||||
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("
|
||||
static void add_str_entry(char** dst, char** src)
|
||||
{
|
||||
size_t len;
|
||||
len = strlen(*src) + strlen(*dst) + 1;
|
||||
|
||||
*dst = malloc(sizeof(char) * len);
|
||||
|
||||
*dst = strcat(*dst, *src);
|
||||
}
|
||||
|
||||
static void add_int_entry(char** dst, int src)
|
||||
{
|
||||
size_t len_int, len;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
#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)
|
||||
{
|
||||
@ -67,4 +108,5 @@ void add_to_db(book_t* book, sqlite3* db)
|
||||
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
|
||||
snprintf(cmd, strlen(INSERT_STR), "%s", INSERT_STR);
|
||||
|
||||
string_cat_vals(&cmd, book, id);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ typedef struct {
|
||||
char *date_added, *pub_date;
|
||||
char *image_code;
|
||||
|
||||
int year_of_pub, page_len;
|
||||
int page_len;
|
||||
} book_t;
|
||||
|
||||
void setup_db(sqlite3* db);
|
||||
|
Loading…
x
Reference in New Issue
Block a user