Compare commits

...

3 Commits

Author SHA1 Message Date
SuperNovaa41
7d8c8c6a80 fixes missing library from makefile 2025-02-28 17:23:11 -05:00
SuperNovaa41
66c9c45c93 helper function to get newest id 2025-02-28 14:03:12 -05:00
SuperNovaa41
5027238879 setup db function 2025-02-28 13:59:40 -05:00
3 changed files with 40 additions and 2 deletions

View File

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

View File

@ -1,3 +1,37 @@
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>
#include "include/db.h"
static int get_num_rows(sqlite3* db)
{
int rows = 0;
sqlite3_stmt* msg;
sqlite3_prepare(db, "SELECT * FROM books;", -1, &msg, NULL);
while (sqlite3_step(msg) == SQLITE_ROW)
rows++;
sqlite3_finalize(msg);
return rows;
}
void setup_db(sqlite3* db)
{
int err;
char* err_buf = 0;
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, \
image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
0, 0, &err_buf);
if (err != SQLITE_OK) {
fprintf(stderr, "sqlite3_exec: %s\n", err_buf);
sqlite3_free(err_buf);
sqlite3_close(db);
exit(EXIT_FAILURE);
}
}

View File

@ -1,3 +1,5 @@
#include <sqlite3.h>
#ifndef DB_H
#define DB_H
@ -5,9 +7,11 @@ typedef struct {
char *title, *subtitle, *isbn;
char *authors, *subjects, *langs;
char *date_added, *pub_date;
char *image_url;
char *image_code;
int year_of_pub, page_len;
} book_t;
void setup_db(sqlite3* db);
#endif