RicardasSim

c libmysql

Dec 27th, 2018
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 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 <mysql.h>
  33.  
  34. #define DB_HOST "localhost"
  35. #define USER_NAME "testuser"
  36. #define USER_PASSWORD "pass123"
  37. #define DATA_BASE_NAME "testdb"
  38.  
  39. MYSQL mysql;
  40. MYSQL_RES *result;
  41. MYSQL_ROW row;
  42. unsigned int num_fields;
  43. unsigned int num_rows;
  44. unsigned int i;
  45.  
  46. int main (int argc, char **argv)
  47. {
  48.  
  49.     if(!mysql_init(&mysql))
  50.     {
  51.         printf("Error initializing MySQL client\n");
  52.         return 1;
  53.     }
  54.  
  55.     mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4");
  56.    
  57.     if (!mysql_real_connect(&mysql, DB_HOST, USER_NAME, USER_PASSWORD, DATA_BASE_NAME, 0, NULL, 0))
  58.     {
  59.         fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&mysql));
  60.         mysql_close(&mysql);
  61.         mysql_library_end();
  62.         return 1;
  63.     }
  64.  
  65.     char queryString[]="SELECT name FROM exmpltbl";
  66.    
  67.     if (mysql_query(&mysql,queryString))
  68.     {
  69.         fprintf(stderr, "mysql_query failed: Error: %s\n", mysql_error(&mysql));
  70.         mysql_close(&mysql);
  71.         mysql_library_end();
  72.         return 1;
  73.     }
  74.    
  75.     result = mysql_store_result(&mysql);
  76.    
  77.     if (result)
  78.     {
  79.         num_fields = mysql_num_fields(result);
  80.         printf("num_fields: %d\n", num_fields);
  81.        
  82.         num_rows = mysql_affected_rows(&mysql);
  83.         printf("num_rows: %d\n", num_rows);
  84.        
  85.         while ((row = mysql_fetch_row(result)))
  86.         {
  87.             unsigned long *lengths;
  88.             lengths = mysql_fetch_lengths(result);
  89.             for(i = 0; i < num_fields; i++)
  90.             {
  91.                 printf("[%.*s] lenght: %d", (int) lengths[i], row[i] ? row[i] : "NULL", (int) lengths[i]);
  92.             }
  93.             printf("\n");
  94.         }
  95.        
  96.         mysql_free_result(result);
  97.        
  98.     }
  99.     else
  100.     {
  101.         if (mysql_errno(&mysql))
  102.         {
  103.             fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
  104.             mysql_close(&mysql);
  105.             mysql_library_end();
  106.             return 1;
  107.         }
  108.         else if (mysql_field_count(&mysql) == 0)
  109.         {
  110.             printf("query does not return data (it was not a SELECT)\n");
  111.            
  112.             num_rows = mysql_affected_rows(&mysql);
  113.             printf("num_rows: %d\n", num_rows);
  114.         }
  115.     }
  116.    
  117.    
  118.    
  119.     mysql_close(&mysql);
  120.     mysql_library_end();
  121.  
  122.     return 0;
  123. }
  124.  
  125. /*
  126. output:
  127.  
  128. num_fields: 1
  129. num_rows: 5
  130. [Simon] lenght: 5
  131. [Veronica] lenght: 8
  132. [Valeria] lenght: 7
  133. [Robert] lenght: 6
  134. [Eglė] lenght: 5
  135.  */
Advertisement
Add Comment
Please, Sign In to add comment