c - 使用c从单板计算机写入mysql数据库

标签 c database

我想将单板机上的网络数据包读入数据库(准确地说是mysql),单板机和mysql之间通信的代码是用c编写的。请我需要您的帮助来获取一些在线 Material 的有用链接,因为我一直在尝试寻找有用的信息,但尚未产生任何结果。感谢您的理解。

最佳答案

您需要在系统上安装第一个libmysqlclient-dev包(我假设您在linux下),然后您可以修改此代码以满足您的需要:

#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):

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

你可以像这样编译你的程序:

gcc -Wall my_msql_program.c -o my_mysql_program -lmysqlclient

关于c - 使用c从单板计算机写入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11826073/

相关文章:

c - 用于 Windows 7 的 C 和 win32 中的 WebSocket 客户端

c - 从这个缓冲区读取 'safest' 的方法是什么?

如果读取 0 为空,C 返回 null

mysql - 如何在线存储转换为sql的access数据库

如果数组字段计数大于 0,CouchDB 将文档包含在索引中

mysql - 数据库查询失败示例;你能帮我解决问题吗?

C- 参数输入

database - 图遍历 : How do I query for "friends and friends of friends" using Gremlin

php - 当我从 View 表中获取数据时出现 Laravel 错误

c++ - 在哪里可以找到所有数学函数的描述,比如 floorf 和其他函数?