Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mysql.h>
  4. #include <string.h>
  5. /* Compile with gcc -o sqltest $(mysql_config --cflags) sqltest.c $(mysql_config --libs) */
  6.  
  7. int main(int argc, char **argv){
  8.  
  9.     MYSQL *mysql;
  10.     MYSQL_RES *result;
  11.     MYSQL_ROW row;
  12.     MYSQL_FIELD *fields;
  13.    
  14.     //DB info
  15.    char *server = "localhost";
  16.    char *user = "root";
  17.    char *password = "xxxxxxx";
  18.    char *database = "xxxxxxx";
  19.    int status;
  20.    
  21.     mysql = mysql_init(NULL);
  22.  
  23.     if (mysql_real_connect (mysql, server, user, password,
  24.          database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL){
  25.       printf("mysql_real_connect() failed\n");
  26.       mysql_close(mysql);
  27.       exit(1);
  28.     }
  29.    
  30.     FILE *f = fopen("headertest2", "r");
  31.     unsigned long long imei;
  32.     unsigned int type=0, size=0, start=0, stop=0, container=0, width=0,height=0, fps=0, vCodec=0, aCodec=0, aBitrate=0, aSampleRate=0;
  33.  
  34.     printf("Read %d bytes\n",fread(&imei, 8, 1, f));
  35.    
  36.     int next;
  37.     printf("Read %d item\n",fread(&next, 4, 1, f));
  38.     type = (next>>30) & 0x2;
  39.     size = next & 0x3FFFFFFF;
  40.  
  41.     printf("Read %d item\n",fread(&next, 4, 1, f));
  42.     start = next;
  43.  
  44.     printf("Read %d item\n",fread(&next, 4, 1, f));
  45.     stop = next;
  46.  
  47.     printf("Read %d item\n",fread(&next, 4, 1, f));
  48.     container = (next>>30) & 0x2;
  49.     width = (next>>18) & 0xFFF;
  50.     height = (next>>6) & 0xFFF;
  51.     fps = next & 0x3F;
  52.  
  53.     printf("Read %d item\n",fread(&next, 4, 1, f));
  54.     vCodec = (next>>28) & 0xF;
  55.     aCodec = (next>>24) & 0xF;
  56.     aBitrate = (next>>15) & 0x1FF;
  57.     aSampleRate = next & 0x7FFF;
  58.    
  59.     status = add_values(mysql, "3", "1000", "video", width, height, container, vCodec, aCodec, fps, aBitrate, aSampleRate);
  60.     printf("IMEI: %llu\n",imei);
  61.     printf("Type: %u\n",type);
  62.     printf("Message Size: %u\n",size);
  63.     printf("Start Time: %u\n",start);
  64.     printf("Stop Time: %u\n",stop);
  65.     printf("Container: %u\n",container);
  66.     printf("Width: %u\n",width);
  67.     printf("Height: %u\n",height);
  68.     printf("FPS: %u\n",fps);
  69.     printf("Video Codec: %u\n",vCodec);
  70.     printf("Audio Codec: %u\n",aCodec);
  71.     printf("Audio Bit Rate: %u\n",aBitrate);
  72.     printf("Audio Sample Rate: %u\n",aSampleRate);
  73.  
  74.     if (status){
  75.   printf("Could not execute statement(s)\n");
  76.   mysql_close(mysql);
  77.   exit(0);
  78.     }
  79.  
  80. /* process each statement result */
  81. do {
  82.   /* did current statement return data? */
  83.   result = mysql_store_result(mysql);
  84.   if (result)
  85.   {
  86.     /* yes; process rows and free the result set */
  87.     while ((row = mysql_fetch_row(result)) != NULL)
  88.       printf("%s \n", row[0]);
  89.  
  90.     mysql_free_result(result);
  91.   }
  92.   else          /* no result set or error */
  93.   {
  94.     if (mysql_field_count(mysql) == 0)
  95.     {
  96.       printf("%lld rows affected\n",
  97.             mysql_affected_rows(mysql));
  98.     }
  99.     else  /* some error occurred */
  100.     {
  101.       printf("Could not retrieve result set\n");
  102.       break;
  103.     }
  104.   }
  105.   /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
  106.   if ((status = mysql_next_result(mysql)) > 0)
  107.     printf("Could not execute statement\n");
  108. } while (status == 0);
  109.  
  110.     mysql_close(mysql);
  111.  
  112.     return(EXIT_SUCCESS);
  113. }
  114. //status = mysql_query(mysql, "INSERT INTO `helios_content` (device_id, file_name, recording_mode, width, height, extension, video_codec, audio_codec, fps, audio_bitrate, audio_sampling_rate) VALUES('2', '1530', 'video', '352', '288', 'mp4', 'h263', 'aac', '15', '32', '3500')");
  115.  
  116. int add_values(MYSQL *mysql, char device_id[], char file_name[], char recording_mode[], unsigned int iwidth, unsigned int iheight, unsigned int iwxtention, unsigned int ivideo_codec, unsigned int iaudio_codec, unsigned int ifps, unsigned int iaudio_bitrate, unsigned int iaudio_sampling_rate){
  117.     int status;
  118.     char query[1024] = """INSERT INTO helios_content (device_id, file_name, recording_mode, width, height, extension, video_codec, audio_codec, fps, audio_bitrate, audio_sampling_rate) VALUES(";
  119.  
  120.     strcat(query, device_id);
  121.     strcat(query, ", ");
  122.     strcat(query, file_name);
  123.     strcat(query, ", ");
  124.     strcat(query, recording_mode);
  125.     strcat(query, ", ");
  126.     //Segmentation fault here
  127.     strcat(query, width);
  128.     strcat(query, ", ");
  129.     strcat(query, height);
  130.     strcat(query, ", ");
  131.     strcat(query, extention);
  132.     strcat(query, ", ");
  133.     strcat(query, video_codec);
  134.     strcat(query, ", ");
  135.     strcat(query, audio_codec);
  136.     strcat(query, ", ");
  137.     strcat(query, fps);
  138.     strcat(query, ", ");
  139.     strcat(query, audio_bitrate);
  140.     strcat(query, ", ");
  141.     strcat(query, audio_sampling_rate);
  142.     strcat(query, ", )""");
  143.     status = mysql_query(mysql, query);
  144.     return status;
  145.    
  146.     }
  147. int get_values(MYSQL *mysql, char table[], char colToLook[], char valueWanted[], char variable[]){
  148.     int status;
  149.     char query[128] = """SELECT ";
  150.     strcat(query, valueWanted);
  151.     strcat(query, " FROM ");
  152.     strcat(query, table);
  153.     strcat(query, " WHERE ");
  154.     strcat(query, colToLook);
  155.     strcat(query, "=");
  156.     strcat(query, variable);
  157.     strcat(query, " LIMIT 0 , 30""");
  158.     printf("%s\n",query);
  159.  
  160.     status = mysql_query(mysql, query);
  161.     return status;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement