写入到mysql数据库从单板计算机采用c [英] Writing into mysql database from a single board computer using c

查看:184
本文介绍了写入到mysql数据库从单板计算机采用c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会想读取单板计算机网络数据包到一个数据库(MySQL的是precise),code的单板计算机和MySQL之间沟通是用C写的。请我需要你在得到一些材料有用的链接在线帮助,因为我一直在努力去寻找有用的信息,但没有产生任何结果呢。感谢您的理解。

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.

推荐答案

您需要在您的系统上安装第一的libmysqlclient-dev软件包(我假设你在linux下),那么你可以修改此code,以满足您的需要:

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);
}

不要忘了链接到mysqlclient库: -lmysqlclient

编辑:

您可以在这样的Debian安装的libmysqlclient-dev的(http://packages.debian.org/squeeze/libmysqlclient-dev):

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

这篇关于写入到mysql数据库从单板计算机采用c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆