diff options
| author | yuzu-eva <cafebabe@disroot.org> | 2025-05-02 20:54:23 +0200 |
|---|---|---|
| committer | yuzu-eva <cafebabe@disroot.org> | 2025-05-02 20:54:23 +0200 |
| commit | 3731ed237f1c983000f07ad585e82f603db69ccd (patch) | |
| tree | 4cbba62777b2ffdf17d539bad76b7496c880dee5 | |
| parent | 29410e3943e3e0da6279708d19b4459578b49f5d (diff) | |
fixed broken queries
| -rw-r--r-- | dbhandling.c | 67 | ||||
| -rw-r--r-- | dbhandling.h | 5 | ||||
| -rw-r--r-- | main.c | 8 |
3 files changed, 60 insertions, 20 deletions
diff --git a/dbhandling.c b/dbhandling.c index a112b8e..d990102 100644 --- a/dbhandling.c +++ b/dbhandling.c @@ -8,6 +8,21 @@ void exit_with_error(sqlite3 *db, const char *msg) exit(69); } +/* + TODO: write function for preparing the statement + Should take entry_t *entry, args_e target (,args_e mode?) + I already differentiate the mode in main, so passing it here + would be redundant. Find another solution. +*/ + +/*POSSIBLE OTHER SOLUTION: + TODO: try to get rid of args_e target and args_e mode and instead + use a flag. 3 possible tables with 3 different modes = 9 bits. Maybe + do 16bits in preparation for additional modes and tables. Not sure how + to go about setting them, though... that will be one hell of an + if-else block, lmao. +*/ + void select_from_table(sqlite3 *db, args_e target, entry_t *entry) { char *sql; @@ -18,7 +33,7 @@ void select_from_table(sqlite3 *db, args_e target, entry_t *entry) const char *type; printf("Name: "); - fgets(entry->name, sizeof(entry->name), stdin); + fgets(entry->name, MAX_NAME_LEN, stdin); // remove newline char, otherwise it messes with the query entry->name[strcspn(entry->name, "\n")] = 0; @@ -90,38 +105,50 @@ void update_entry(sqlite3 *db, args_e target, entry_t *entry) sqlite3_stmt *stmt; printf("Name: "); - fgets(entry->name, sizeof(entry->name), stdin); + fgets(entry->name, MAX_NAME_LEN, stdin); + + // remove newline char, otherwise it messes with the query + entry->name[strcspn(entry->name, "\n")] = 0; + + // status is currently never NULL, so the if-else makes no sense + // would be addressed by the TODO: switching to flags switch (target) { case ANIME: printf("Episode: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; if (entry->status == NULL) { sql = "UPDATE anime SET episode=?1 WHERE name=?2;"; } else { printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "UPDATE anime SET episode=?1, status=?2 WHERE name=?3;"; } break; case MANGA: printf("Chapter: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; if (entry->status == NULL) { sql = "UPDATE manga SET chapter=?1 WHERE name=?2;"; } else { printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "UPDATE manga SET chapter=?1, status=?2 WHERE name=?3;"; } break; case BOOK: printf("Chapter: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; if (entry->status == NULL) { sql = "UPDATE book SET chapter=?1 WHERE name=?2;"; } else { printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "UPDATE book SET chapter=?1, status=?2 WHERE name=?3;"; } break; @@ -161,29 +188,37 @@ void add_entry(sqlite3 *db, args_e target, entry_t *entry) sqlite3_stmt *stmt; printf("Name: "); - fgets(entry->name, sizeof(entry->name), stdin); + fgets(entry->name, MAX_NAME_LEN, stdin); + entry->name[strcspn(entry->name, "\n")] = 0; switch (target) { case ANIME: printf("Episode: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "INSERT INTO anime (name, episode, status) VALUES (?1, ?2, ?3);"; break; case MANGA: printf("Chapter: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "INSERT INTO manga (name, chapter, status) VALUES (?1, ?2, ?3);"; break; case BOOK: printf("Author: "); - fgets(entry->author, sizeof(entry->author), stdin); + fgets(entry->author, MAX_AUTHOR_LEN, stdin); + entry->author[strcspn(entry->author, "\n")] = 0; printf("Chapter: "); - fgets(entry->value, sizeof(entry->value), stdin); + fgets(entry->value, MAX_VALUE_LEN, stdin); + entry->value[strcspn(entry->value, "\n")] = 0; printf("Status: "); - fgets(entry->status, sizeof(entry->status), stdin); + fgets(entry->status, MAX_STATUS_LEN, stdin); + entry->status[strcspn(entry->status, "\n")] = 0; sql = "INSERT INTO book (name, author, chapter, status) VALUES (?1, ?2, ?3, ?4);"; break; default: diff --git a/dbhandling.h b/dbhandling.h index e9b28d2..fd21579 100644 --- a/dbhandling.h +++ b/dbhandling.h @@ -9,6 +9,11 @@ #include "enum.h" +#define MAX_NAME_LEN 128 +#define MAX_AUTHOR_LEN 64 +#define MAX_VALUE_LEN 16 +#define MAX_STATUS_LEN 16 + typedef struct { char *name; char *author; @@ -34,10 +34,10 @@ int main(int argc, char **argv) args_e target; target = str2enum(argv[2]); entry_t *entry = malloc(sizeof(entry_t)); - entry->name = malloc(128 * sizeof(char)); - entry->author = malloc(64 * sizeof(char)); - entry->value = malloc(16 * sizeof(char)); - entry->status = malloc(16 * sizeof(char)); + entry->name = malloc(MAX_NAME_LEN); + entry->author = malloc(MAX_AUTHOR_LEN); + entry->value = malloc(MAX_VALUE_LEN); + entry->status = malloc(MAX_STATUS_LEN); char fullpath[PATH_MAX]; snprintf(fullpath, PATH_MAX, "%s/%s/%s", getenv("HOME"), filepath, filename); |
