Advertisement
Guest User

Untitled

a guest
Jul 28th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <mysql/mysql.h>
  5. #include <stdbool.h>
  6.  
  7. #define STRING_SIZE 50
  8. #define INSERT_SAMPLE "INSERT INTO analytics.live (dat_sent, machine_id, foreign_addr, con_state) VALUES (?,?,?,?)"
  9.  
  10. /*  Compile with:
  11.         gcc db.connect.c `mysql_config --libs` -O1
  12.         for the best results
  13. */
  14.  
  15. int main(int argc, char ** argv)
  16. {
  17.  
  18.     MYSQL_STMT    *stmt;
  19.     MYSQL_BIND    bind[4];
  20.     unsigned long affected_rows;
  21.     int           param_count;
  22.     short         small_data;
  23.     int           int_data;
  24.     char          str_data[STRING_SIZE];
  25.     unsigned long str_length;
  26.     int           is_null;
  27.  
  28.  
  29.     /*
  30.      * Prepare an INSERT query with 4 parameters
  31.     */
  32.  
  33.     MYSQL *mysql = mysql_init(NULL);
  34.  
  35.     if (mysql_real_connect(mysql, "localhost", "user", "password}",
  36.        NULL, 0, NULL, 0) == NULL){
  37.         fprintf(stderr, "%s\n", mysql_error(mysql));
  38.         mysql_close(mysql);
  39.         exit(1);
  40.  
  41.     }
  42.  
  43.  
  44.     stmt = mysql_stmt_init(mysql);
  45.     if (!stmt){
  46.         fprintf(stderr, " mysql_stmt_init(), out of memory\n");
  47.         return 0;
  48.     }
  49.  
  50.  
  51.     if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE))){
  52.         fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
  53.         fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  54.         return 0;
  55.     }
  56.  
  57.     fprintf(stdout, " prepare, INSERT successful\n");
  58.  
  59.     /* Get the parameter count from the statement */
  60.     param_count= mysql_stmt_param_count(stmt);
  61.     fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
  62.  
  63.     if (param_count != 4) /* validate parameter count */
  64.     {
  65.         fprintf(stderr, " invalid parameter count returned by MySQL\n");
  66.         return 0;
  67.     }
  68.  
  69.     /* Bind data for all 4 parameters */
  70.     memset(bind, 0, sizeof(bind));
  71.  
  72.     /* INTEGER PARAM */
  73.     bind[0].buffer_type= MYSQL_TYPE_LONG;
  74.     bind[0].buffer= (char *)&int_data;
  75.     bind[0].is_null= 0;
  76.     bind[0].length= 0;
  77.  
  78.     /* STRING PARAM */
  79.     bind[1].buffer_type= MYSQL_TYPE_STRING;
  80.     bind[1].buffer= (char *)str_data;
  81.     bind[1].buffer_length= STRING_SIZE;
  82.     bind[1].is_null= 0;
  83.     bind[1].length= &str_length;
  84.  
  85.     /* STRING PARAM */
  86.     bind[2].buffer_type= MYSQL_TYPE_STRING;
  87.     bind[2].buffer= (char *)str_data;
  88.     bind[2].buffer_length= STRING_SIZE;
  89.     bind[2].is_null= 0;
  90.     bind[2].length= &str_length;
  91.  
  92.     /* STRING PARAM */
  93.     bind[3].buffer_type= MYSQL_TYPE_STRING;
  94.     bind[3].buffer= (char *)str_data;
  95.     bind[3].buffer_length= STRING_SIZE;
  96.     bind[3].is_null= 0;
  97.     bind[3].length= &str_length;
  98.  
  99.     if (mysql_stmt_bind_param(stmt, bind)){
  100.         fprintf(stderr, " mysql_stmt_bind_param() failed\n");
  101.         fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  102.         return 0;
  103.     }
  104.  
  105.     /* Specify the data values for the first row */
  106.     int_data= argv[1];             /* integer */
  107.     strncpy(str_data, "989b3ff047184022b33bd395a97cde4c", STRING_SIZE); /* string  */
  108.     str_length= strlen(str_data);
  109.  
  110.     /* INSERT SMALLINT data as NULL */
  111.     is_null= 1;
  112.  
  113.     /* Execute the INSERT statement - 1*/
  114.     if (mysql_stmt_execute(stmt))
  115.     {
  116.         fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
  117.         fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  118.         return 0;
  119.     }
  120.  
  121.  
  122.     /* Get the number of affected rows */
  123.     affected_rows= mysql_stmt_affected_rows(stmt);
  124.     fprintf(stdout, " total affected rows(insert 1): %lu\n",
  125.                     (unsigned long) affected_rows);
  126.  
  127.  
  128.     /*validate affected rows*/
  129.     if (affected_rows != 1) {
  130.         fprintf(stderr, "Invalid affected rows by MySQL \n");
  131.         return 0;
  132.     }
  133.  
  134.     int_data = 33;
  135.     strncpy(str_data, "machine_id_goes_in_here", STRING_SIZE);
  136.     str_length = strlen(str_data);
  137.     small_data = 1000;
  138.     is_null = 0;
  139.  
  140.     /* Executre ithe insert statement*/
  141.     if (mysql_stmt_execute(stmt)){
  142.         fprintf(stderr, "mysql_stmt_execute, 2 failde \n" );
  143.         fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
  144.         return 0;
  145.     }
  146.  
  147.     affected_rows = mysql_stmt_affected_rows(stmt);
  148.     fprintf(stdout, "total affected rows(insert 2): %lu\n",(unsigned long) affected_rows);
  149.     if (affected_rows != 1){
  150.         fprintf(stderr, "invalid rows affected by MySQL\n");
  151.         return 0;
  152.     }
  153.  
  154.     if (mysql_stmt_close(stmt)){
  155.           fprintf(stderr, " failed while closing the statement\n");
  156.           fprintf(stderr, " %s\n", mysql_error(mysql));
  157.           return 0;
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement