Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include "user.h"
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sqlite3.h>
  7.  
  8. static int find_user_callback(void *, int, char **, char **);
  9.  
  10. User *empty_user() {
  11. User *new = (User *)malloc(sizeof(User *));
  12. new->firstname = (char *)malloc(sizeof(char) * 16);
  13. new->lastname = (char *)malloc(sizeof(char) * 16);
  14. new->username = (char *)malloc(sizeof(char) * 16);
  15. new->password = (char *)malloc(sizeof(char) * 16);
  16. new->sex = 0;
  17. return new;
  18. }
  19.  
  20. User *create_user(char *firstname, char *lastname, char *username,
  21. char *password, Sex sex, char birthday[11]) {
  22. User *new = empty_user();
  23. strcpy(new->firstname, firstname);
  24. strcpy(new->lastname, lastname);
  25. strcpy(new->username, username);
  26. strcpy(new->password, password);
  27. strcpy(new->birthday, birthday);
  28. new->sex = sex;
  29.  
  30. return new;
  31. }
  32.  
  33.  
  34. User *find_user(char *username, char *password) {
  35. sqlite3 *db;
  36. int rc = sqlite3_open("cinder.db", &db);
  37. if(rc) {
  38. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  39. return NULL;
  40. }
  41.  
  42. char sql[] = "SELECT * FROM users WHERE username='Necros' AND password='123'";
  43.  
  44. User *user = empty_user();
  45. rc = sqlite3_exec(db, sql, find_user_callback, (void *)user, NULL);
  46. printf("%s\n", user->firstname);
  47. printf("End !\n");
  48. return user;
  49. }
  50.  
  51. static int find_user_callback(void *user_pt, int argc, char **argv, char **azColName){
  52. printf("callback\n");
  53. User *user = (User *)user_pt;
  54. strcpy(user->firstname, argv[1]);
  55. printf("%s\n", argv[1]);
  56. for(int i=0; i < argc; i++){
  57. printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  58. }
  59. printf("\n");
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement