日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c 获取数据库数据计算机,使用c从单板计算机写入mysql数据库使用c

發(fā)布時(shí)間:2025/3/15 数据库 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 获取数据库数据计算机,使用c从单板计算机写入mysql数据库使用c 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

您需要在系統(tǒng)上安裝第一的libmysqlclient-dev軟件包(我假設(shè)你在linux下),那么你可以修改這個(gè)代碼,以滿足您的需要:

#include

#include

#include

#include

#include

#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

總結(jié)

以上是生活随笔為你收集整理的c 获取数据库数据计算机,使用c从单板计算机写入mysql数据库使用c的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。