diff options
Diffstat (limited to 'db.c')
-rw-r--r-- | db.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -0,0 +1,36 @@ +#include <err.h> +#include <stdlib.h> +#include <sqlite3.h> +#include "db.h" + +struct user * +getuser(int id) +{ + sqlite3_stmt *stmt; + struct user *user; + + if(sqlite3_prepare(db, + "SELECT name, hash FROM users WHERE oid = ?", + -1, &stmt, 0) != SQLITE_OK) + goto null; + + if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) + goto null; + + if(sqlite3_step(stmt) != SQLITE_ROW) + goto null; + + if(!(user = malloc(sizeof(struct user)))) + err(1, "malloc"); + + user->id = id; + user->name = sqlite3_column_text(stmt, 0); + user->hash = sqlite3_column_text(stmt, 1); + + sqlite3_finalize(stmt); + return user; + +null: + sqlite3_finalize(stmt); + return NULL; +}
\ No newline at end of file |