Connect c under mysql to mysql database
1. install sudo yum install mysql-devel to install components and libraries
2.
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mysql/mysql.h>int main(int argc, const char *argv[]){ MYSQL mysql; MYSQL_RES *result; MYSQL_ROW row; mysql_init(&mysql); mysql_real_connect(&mysql, "127.0.0.1", "syswj", "123456", "sysDB", 0, NULL, 0 ); mysql_query(&mysql, "select * from stu"); result = mysql_store_result(&mysql); while((row = mysql_fetch_row(result))) { printf("%s %s %s %s %s\n", row[0], row[1], row[2], row[3], row[4], row[5]); } mysql_free_result(result); mysql_close(&mysql); return 0;}
3. Compile: gcc test-L/usr/lib/mysql-lmysqlclient
4. Execute:
[Syswj @ host document] $. /. out 950 LI Yong m 20 CS951 Zhang San f 23 EN952 Li Si f 24 DC953 Large f 14 DC954 large m 27 CS955 Hello f 27 CC [syswj @ host document] $
Reference: http://www.linuxidc.com/linux/2011-12/49133.htm ?#