45 lines
1.0 KiB
C
45 lines
1.0 KiB
C
#include "database.h"
|
|
#include "sql.h"
|
|
#include "user.h"
|
|
|
|
bool db_delete_user(const int id) {
|
|
pthread_mutex_lock(&db_mutex);
|
|
if (!db) {
|
|
fprintf(stderr, "Database not initialized\n");
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return false;
|
|
}
|
|
sqlite3_stmt *stmt;
|
|
int rc;
|
|
|
|
rc = sqlite3_prepare_v2(db, DELETE_USER_SQL, -1, &stmt, 0);
|
|
if (rc != SQLITE_OK) {
|
|
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return false;
|
|
}
|
|
|
|
sqlite3_bind_int(stmt, 1, id);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
if (rc != SQLITE_DONE) {
|
|
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
|
sqlite3_finalize(stmt);
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return false;
|
|
}
|
|
|
|
int changes = sqlite3_changes(db);
|
|
sqlite3_finalize(stmt);
|
|
|
|
if (changes > 0) {
|
|
printf("User deleted successfully. ID: %d\n", id);
|
|
} else {
|
|
printf("No user found with ID: %d\n", id);
|
|
return false;
|
|
}
|
|
|
|
pthread_mutex_unlock(&db_mutex);
|
|
return true;
|
|
}
|