Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <mysql.h>
- #include <string.h>
- /* Compile with gcc -o sqltest $(mysql_config --cflags) sqltest.c $(mysql_config --libs) */
- int main(int argc, char **argv){
- MYSQL *mysql;
- MYSQL_RES *result;
- MYSQL_ROW row;
- MYSQL_FIELD *fields;
- //DB info
- char *server = "localhost";
- char *user = "root";
- char *password = "xxxxxxx";
- char *database = "xxxxxxx";
- int status;
- mysql = mysql_init(NULL);
- if (mysql_real_connect (mysql, server, user, password,
- database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL){
- printf("mysql_real_connect() failed\n");
- mysql_close(mysql);
- exit(1);
- }
- FILE *f = fopen("headertest2", "r");
- unsigned long long imei;
- 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;
- printf("Read %d bytes\n",fread(&imei, 8, 1, f));
- int next;
- printf("Read %d item\n",fread(&next, 4, 1, f));
- type = (next>>30) & 0x2;
- size = next & 0x3FFFFFFF;
- printf("Read %d item\n",fread(&next, 4, 1, f));
- start = next;
- printf("Read %d item\n",fread(&next, 4, 1, f));
- stop = next;
- printf("Read %d item\n",fread(&next, 4, 1, f));
- container = (next>>30) & 0x2;
- width = (next>>18) & 0xFFF;
- height = (next>>6) & 0xFFF;
- fps = next & 0x3F;
- printf("Read %d item\n",fread(&next, 4, 1, f));
- vCodec = (next>>28) & 0xF;
- aCodec = (next>>24) & 0xF;
- aBitrate = (next>>15) & 0x1FF;
- aSampleRate = next & 0x7FFF;
- status = add_values(mysql, "3", "1000", "video", width, height, container, vCodec, aCodec, fps, aBitrate, aSampleRate);
- printf("IMEI: %llu\n",imei);
- printf("Type: %u\n",type);
- printf("Message Size: %u\n",size);
- printf("Start Time: %u\n",start);
- printf("Stop Time: %u\n",stop);
- printf("Container: %u\n",container);
- printf("Width: %u\n",width);
- printf("Height: %u\n",height);
- printf("FPS: %u\n",fps);
- printf("Video Codec: %u\n",vCodec);
- printf("Audio Codec: %u\n",aCodec);
- printf("Audio Bit Rate: %u\n",aBitrate);
- printf("Audio Sample Rate: %u\n",aSampleRate);
- if (status){
- printf("Could not execute statement(s)\n");
- mysql_close(mysql);
- exit(0);
- }
- /* process each statement result */
- do {
- /* did current statement return data? */
- result = mysql_store_result(mysql);
- if (result)
- {
- /* yes; process rows and free the result set */
- while ((row = mysql_fetch_row(result)) != NULL)
- printf("%s \n", row[0]);
- mysql_free_result(result);
- }
- else /* no result set or error */
- {
- if (mysql_field_count(mysql) == 0)
- {
- printf("%lld rows affected\n",
- mysql_affected_rows(mysql));
- }
- else /* some error occurred */
- {
- printf("Could not retrieve result set\n");
- break;
- }
- }
- /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
- if ((status = mysql_next_result(mysql)) > 0)
- printf("Could not execute statement\n");
- } while (status == 0);
- mysql_close(mysql);
- return(EXIT_SUCCESS);
- }
- //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')");
- 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){
- int status;
- 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(";
- strcat(query, device_id);
- strcat(query, ", ");
- strcat(query, file_name);
- strcat(query, ", ");
- strcat(query, recording_mode);
- strcat(query, ", ");
- //Segmentation fault here
- strcat(query, width);
- strcat(query, ", ");
- strcat(query, height);
- strcat(query, ", ");
- strcat(query, extention);
- strcat(query, ", ");
- strcat(query, video_codec);
- strcat(query, ", ");
- strcat(query, audio_codec);
- strcat(query, ", ");
- strcat(query, fps);
- strcat(query, ", ");
- strcat(query, audio_bitrate);
- strcat(query, ", ");
- strcat(query, audio_sampling_rate);
- strcat(query, ", )""");
- status = mysql_query(mysql, query);
- return status;
- }
- int get_values(MYSQL *mysql, char table[], char colToLook[], char valueWanted[], char variable[]){
- int status;
- char query[128] = """SELECT ";
- strcat(query, valueWanted);
- strcat(query, " FROM ");
- strcat(query, table);
- strcat(query, " WHERE ");
- strcat(query, colToLook);
- strcat(query, "=");
- strcat(query, variable);
- strcat(query, " LIMIT 0 , 30""");
- printf("%s\n",query);
- status = mysql_query(mysql, query);
- return status;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement