SHARE
TWEET

Untitled

a guest May 19th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. pregunta?
  4.  
  5. Que opciones deberia pasarle a mysql_options(), para que esto no suceda?
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <mysql.h>
  13.  
  14. #define     SERVER      "localhost"
  15. #define     USER        "root"
  16. #define     DATABASE    "LIBRERIA"
  17. #define     PASSWORD    "xf5bjm1sp3z"
  18.  
  19. #define     QUERY       "SELECT empresa,libro FROM main WHERE MATCH(empresa, libro, categoria, editora, autor, precio) AGAINST(\'%s\'" IN BOOLEAN MODE) ORDER BY RAND()"
  20.  
  21. int
  22. main(int argc, char *argv[])
  23. {
  24.     size_t      length;
  25.     char        *mybuf;
  26.     MYSQL       *conn;
  27.     MYSQL_RES   *res;
  28.     MYSQL_ROW   row;
  29.  
  30.     if(argc!=2) {
  31.         fprintf(stderr, "Uso: %s < frase >\n", argv[0]);
  32.         exit(EXIT_FAILURE);
  33.     }
  34.  
  35.     conn = mysql_init(NULL);
  36.  
  37.     if (mysql_options(conn, MYSQL_SET_CHARSET_NAME, "utf8") != 0)  {
  38.         fprintf(stderr, "%s\n", mysql_error(conn));
  39.         exit(EXIT_FAILURE);
  40.     }
  41.  
  42.     if (!mysql_real_connect(conn, SERVER, USER, PASSWORD, DATABASE, 0, NULL, 0)) {
  43.         fprintf(stderr, "%s\n", mysql_error(conn));
  44.         exit(EXIT_FAILURE);
  45.     }
  46.  
  47.     length = strlen(QUERY) + strlen(argv[1]);
  48.  
  49.     mybuf = (char*)malloc(length);
  50.  
  51.     puts(mysql_character_set_name(conn));
  52.  
  53.     (void)snprintf(&mybuf[0], length, QUERY, argv[1]);
  54.  
  55.     if (mysql_query(conn, &mybuf[0])) {
  56.         fprintf(stderr, "%s\n", mysql_error(conn));
  57.         exit(EXIT_FAILURE);
  58.     }
  59.  
  60.     res = mysql_use_result(conn);
  61.  
  62.     while ((row = mysql_fetch_row(res)) != NULL)
  63.         printf("%s\t\t%s\n", row[0], row[1]);
  64.  
  65.     /* Release memory used to store results and close connection */
  66.     mysql_free_result(res);
  67.     mysql_close(conn);
  68.  
  69.    return 0;
  70. }
RAW Paste Data
Top