RicardasSim

c libmysql mysql_real_escape_string

Dec 28th, 2018
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. /*
  2.  
  3.  mysql> CREATE DATABASE testdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  4.  mysql> USE testdb;
  5.  mysql> CREATE TABLE exmpltbl ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_exmpltbl primary key (id) );
  6.  mysql> INSERT INTO exmpltbl ( id, name ) VALUES ( null, 'Simon' );
  7.  mysql> INSERT INTO exmpltbl ( id, name ) VALUES ( null, 'Veronica' );
  8.  mysql> INSERT INTO exmpltbl ( id, name ) VALUES ( null, 'Valeria' );
  9.  mysql> INSERT INTO exmpltbl ( id, name ) VALUES ( null, 'Robert' );
  10.  mysql> INSERT INTO exmpltbl ( id, name ) VALUES ( null, 'Eglė' );
  11.  
  12.  mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'pass123';
  13.  mysql> GRANT ALL PRIVILEGES ON testdb . * TO 'testuser'@'localhost';
  14.  mysql> FLUSH PRIVILEGES;
  15.  
  16.  mysql> quit
  17.  
  18.  build:
  19.  
  20.  gcc -Wall -Wextra -Wpedantic -I/usr/include/mysql -c -o main.o main.c
  21.  gcc -o test_mysql main.o -L/usr/lib64/mysql -lmysqlclient
  22.  
  23.  or:
  24.  
  25.  gcc -Wall -Wextra -Wpedantic `mysql_config --cflags` -c -o main.o main.c
  26.  gcc -o test_mysql main.o `mysql_config --libs`
  27.  
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <mysql.h>
  34.  
  35. #define DB_HOST "localhost"
  36. #define USER_NAME "testuser"
  37. #define USER_PASSWORD "pass123"
  38. #define DATA_BASE_NAME "testdb"
  39.  
  40. MYSQL mysql;
  41. MYSQL_RES *result;
  42. MYSQL_ROW row;
  43. unsigned int num_fields;
  44. unsigned int num_rows;
  45. unsigned int i;
  46.  
  47. int main (int argc, char **argv)
  48. {
  49.    
  50.     if(!mysql_init(&mysql))
  51.     {
  52.         printf("Error initializing MySQL client\n");
  53.         return 1;
  54.     }
  55.  
  56.     mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4");
  57.    
  58.     if (!mysql_real_connect(&mysql, DB_HOST, USER_NAME, USER_PASSWORD, DATA_BASE_NAME, 0, NULL, 0))
  59.     {
  60.         fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&mysql));
  61.         mysql_close(&mysql);
  62.         mysql_library_end();
  63.         return 1;
  64.     }
  65.    
  66.    
  67.     char testStr[]="a";
  68.    
  69.     char queryString[1000],*end;
  70.    
  71.     end = stpcpy(queryString,"SELECT name FROM exmpltbl WHERE name LIKE '%");
  72.     end += mysql_real_escape_string(&mysql,end,testStr,strlen(testStr));
  73.     end = stpcpy(end,"%'");
  74.    
  75.     if (mysql_real_query(&mysql,queryString,(unsigned int) (end - queryString)))
  76.     {
  77.         fprintf(stderr, "mysql_query failed: Error: %s\n", mysql_error(&mysql));
  78.         mysql_close(&mysql);
  79.         mysql_library_end();
  80.         return 1;
  81.     }
  82.    
  83.     result = mysql_store_result(&mysql);
  84.    
  85.     if (result)
  86.     {
  87.         num_fields = mysql_num_fields(result);
  88.         printf("num_fields: %d\n", num_fields);
  89.        
  90.         num_rows = mysql_affected_rows(&mysql);
  91.         printf("num_rows: %d\n", num_rows);
  92.        
  93.         while ((row = mysql_fetch_row(result)))
  94.         {
  95.             unsigned long *lengths;
  96.             lengths = mysql_fetch_lengths(result);
  97.             for(i = 0; i < num_fields; i++)
  98.             {
  99.                 printf("[%.*s]", (int) lengths[i], row[i] ? row[i] : "NULL");
  100.             }
  101.             printf("\n");
  102.         }
  103.    
  104.         mysql_free_result(result);
  105.    
  106.     }
  107.     else
  108.     {
  109.         if (mysql_errno(&mysql))
  110.         {
  111.             fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
  112.             mysql_close(&mysql);
  113.             mysql_library_end();
  114.             return 1;
  115.         }
  116.         else if (mysql_field_count(&mysql) == 0)
  117.         {
  118.             printf("query does not return data (it was not a SELECT)\n");
  119.            
  120.             num_rows = mysql_affected_rows(&mysql);
  121.             printf("num_rows: %d\n", num_rows);
  122.         }
  123.     }
  124.    
  125.    
  126.     mysql_close(&mysql);
  127.     mysql_library_end();
  128.  
  129.     return 0;
  130. }
  131.  
  132. /*
  133. output:
  134.  
  135. num_fields: 1
  136. num_rows: 2
  137. [Veronica]
  138. [Valeria]
  139.  
  140.  */
Advertisement
Add Comment
Please, Sign In to add comment