Writing to mysql database from one on-board computer using c

I would like to read network packets from one computer to a database (more precisely, mysql), the code for exchanging data between a single-board computer and mysql should be written in c. Please, I need your help in obtaining useful links to some materials on the Internet, as I try to find useful information, but so far I have not brought any result. Thank you for your understanding.

0
source share
2 answers

You need to install the first libmysqlclient-dev package (I assume you are on Linux) on your system, then you can modify this code to suit your needs:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <mysql/mysql.h> #define BUFFER_SIZE 1024 // Increase this buffer if yoy want /* This function is used for the database connection */ MYSQL * my_mysql_connection(const char *server, const char *db, const char *user, const char *pwd) { MYSQL *myh; /* Some initialisation */ if (NULL == (myh = mysql_init(NULL))) { fprintf(stdeee, "Fails to allocate memory for MYSQL!\n"); exit (EXIT_FAILURE); } /* Connect to the database. */ if (NULL == mysql_real_connect (myh, server, user, pwd, db, 0, NULL, 0)) { fprintf(stderr, "%s", mysql_error(myh)); free (myh); return NULL; } return myh; } /* This function is used to perform a query */ int my_mysql_query(MYSQL *myh, const char *query) { /* Do the query request */ if (0 != mysql_query(myh, query)) { fprintf(stderr, "FAIL to perform the query : '%s' %s\n", query, mysql_error(myh)); exit (EXIT_FAILURE); } return 0; } /* * Suppose that your table students_table has this fields : student_number, student_name, * student_address, student_phone */ /* This function is used to get and process the result of the query */ void my_mysql_process_query_result(MYSQL * myh) { int num_fields; int i; MYSQL_RES *query_result; MYSQL_FIELD *field; MYSQL_ROW row; char *buffer; buffer = (char *) calloc(BUFFER_SIZE, sizeof(char)); /* Select all students present in the students_table */ if (my_mysql_query(myh, "SELECT student_number, student_name, student_address, student_phone FROM students_table")) { exit (EXIT_FAILURE); } query_result = mysql_store_result (myh); /* Retreive the number of rows and fields */ field = mysql_fetch_fields(query_result); num_fields = mysql_num_fields(query_result); /* construct the buffer containing each row */ while ((row = mysql_fetch_row (query_result))) { /* Init our buffer with fields sperated by ";", modify if you need, it just an example */ memset(buffer, '\0', sizeof*buffer); for (i = 0; i < num_fields - 1; i++) { strncat(buffer, row[i], strlen(row[i]) + 1); strncat(buffer, ";", 2); } strncat(buffer, row[i], strlen(row[i]) + 1); strncat(buffer, "\n", 2); // You can process your buffer (row) here process_student_row(buffer); } free(buffer); mysql_free_result (query_result); } 

Remember to reference the mysqlclient library: -lmysqlclient .

Edit:

You can install libmysqlclient-dev (http://packages.debian.org/squeeze/libmysqlclient-dev) on debian as follows:

 sudo apt-get update sudo apt-get install libmysqlclient-dev 

You can compile your program as follows:

 gcc -Wall my_msql_program.c -o my_mysql_program -lmysqlclient 
+1
source

If mySQL runs on a regular PC, you need to exchange data between the single-board computer and the regular PC so that the data you want is transferred from the board to the PC. You will need some kind of server on the PC. Having done this, the PC then takes the data and uses the mySQL C API, transfers it to the mySQL database.

Maybe I'm wrong, but you can be in your head. The overall design is simple, and if this is not obvious to you, you might be trying to do something too heavy.

0
source

Source: https://habr.com/ru/post/924666/


All Articles