Implement add and delete actions

This commit is contained in:
augustin64 2023-07-12 21:28:54 +02:00
parent 3696ca684f
commit 33f77d4cfc
2 changed files with 65 additions and 4 deletions

View File

@ -53,12 +53,46 @@ sqlite3* get_db() {
}
int add_task(task_t t) { //TODO
int add_task(task_t t) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, done, due_to) VALUES (?1, ?2, ?3, ?4);", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
return 1;
}
char str_id[10];
sprintf(str_id, "%d", (int)t.id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC);
char str_done[2]; // Just a boolean
sprintf(str_done, "%d", t.done);
sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC);
char str_due_to[15];
sprintf(str_due_to, "%ld", t.due_to);
sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC);
ret = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
(void)t;
return 1;
if (ret != SQLITE_DONE) {
if (ret == 19) {
printf("Identifiant déjà utilisé\n");
} else {
printf("Return value: %d\n", ret);
}
return 1;
}
return 0;
}
int update_task(task_t t) { //TODO
@ -67,6 +101,28 @@ int update_task(task_t t) { //TODO
}
void delete_task(int id) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;
int ret = sqlite3_prepare_v2(db, "DELETE FROM tasks WHERE id=?1;", -1, &stmt, 0);
if (ret != SQLITE_OK) {
printf("(get_task) failure fetching data\n");
exit(1);
}
char str_id[10];
sprintf(str_id, "%d", id);
sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
}
void get_task(int id, task_t* t) {
sqlite3* db = get_db();
sqlite3_stmt* stmt;

View File

@ -24,6 +24,11 @@ Mettre à jour une tâche
*/
int update_task(task_t t);
/*
Supprime la tâche [id] de la base de données. La tâche est supposée existante
*/
void delete_task(int id);
/*
Copie la tâche [id] dans *t si elle existe
*/