Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "user.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sqlite3.h>
- static int find_user_callback(void *, int, char **, char **);
- User *empty_user() {
- User *new = (User *)malloc(sizeof(User *));
- new->firstname = (char *)malloc(sizeof(char) * 16);
- new->lastname = (char *)malloc(sizeof(char) * 16);
- new->username = (char *)malloc(sizeof(char) * 16);
- new->password = (char *)malloc(sizeof(char) * 16);
- new->sex = 0;
- return new;
- }
- User *create_user(char *firstname, char *lastname, char *username,
- char *password, Sex sex, char birthday[11]) {
- User *new = empty_user();
- strcpy(new->firstname, firstname);
- strcpy(new->lastname, lastname);
- strcpy(new->username, username);
- strcpy(new->password, password);
- strcpy(new->birthday, birthday);
- new->sex = sex;
- return new;
- }
- User *find_user(char *username, char *password) {
- sqlite3 *db;
- int rc = sqlite3_open("cinder.db", &db);
- if(rc) {
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- return NULL;
- }
- char sql[] = "SELECT * FROM users WHERE username='Necros' AND password='123'";
- User *user = empty_user();
- rc = sqlite3_exec(db, sql, find_user_callback, (void *)user, NULL);
- printf("%s\n", user->firstname);
- printf("End !\n");
- return user;
- }
- static int find_user_callback(void *user_pt, int argc, char **argv, char **azColName){
- printf("callback\n");
- User *user = (User *)user_pt;
- strcpy(user->firstname, argv[1]);
- printf("%s\n", argv[1]);
- for(int i=0; i < argc; i++){
- printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement