aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/db.c b/db.c
new file mode 100644
index 0000000..fcb742d
--- /dev/null
+++ b/db.c
@@ -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