1

I will like to read network packets from a single board computer into a database(mysql to be precise), code to communicate between the single board computer and mysql is to be written in c. Please I need your help in getting useful links to some materials online, as I have been trying to seek for useful information but not yielding any result yet. Thank you for your understanding.

arewa
  • 95
  • 9
  • 1
    The question is too broad and non-specific to be meaningfully answered. –  Aug 06 '12 at 10:03

2 Answers2

1

You need to install first libmysqlclient-dev package (I assume you're under linux) on your system, then you can modify this code to meet your need:

#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's 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);
}

Don't forget to link to the mysqlclient library : -lmysqlclient.

Edit:

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

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

You can compile your program like this:

gcc -Wall my_msql_program.c -o my_mysql_program -lmysqlclient
TOC
  • 4,326
  • 18
  • 21
  • Yes am working with the debian-linux. Just like you said, I tried to install libmysqlclient15-dev but not succesful. Only mysql-client and mysql-server were installed. Thanks so much for your help, please you can still put me through further. – arewa Aug 06 '12 at 14:22
  • @arewa : why installing libmysqlclient15-dev? i will edit my answer – TOC Aug 06 '12 at 14:28
  • Once again, thanks so much for those information you gave as they were so helpful. Mysql is now in place, just working on the code (which you have also helped with sample code) to get those data from the single board computer to the database. I really appreciate your effort. – arewa Aug 08 '12 at 06:24
  • How do you do it with prepared statements where both (1) results and (2) where clause is bound? – jww Feb 20 '19 at 01:00
0

If mySQL is running on a normal PC, then you need to communicate between the single board computer and the normal PC, such that the data you wish for is transferred from the board to the PC. You'll need a server of some kind on the PC. Having done that, the PC then takes the data and using the mySQL C API, commits it to the mySQL database.

I may be wrong, but you may be in over your head. The general design is simple and if it isn't obvious to you, it may be that you're trying to do something too hard.