Linux 下C語言連接mysql實(shí)例詳解
Linux 下C語言連接mysql實(shí)例詳解
第一步:
安裝mysql, 參考:http://www.dbjr.com.cn/article/39190.htm
第二步:
安裝mysql.h函數(shù)庫
sudo apt-get install libmysqlclient-dev
執(zhí)行之后就可以看到/usr/include/MySQL目錄了
然后開始我們的鏈接.
首先看我的數(shù)據(jù)庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chat_room | | mysql | | mysql_shiyan | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> use chat_room; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------+ | Tables_in_chat_room | +---------------------+ | user_message | +---------------------+ 1 row in set (0.00 sec) mysql> select * from user_message; +------+-------+--------+ | ID | name | passwd | +------+-------+--------+ | 1 | linux | linux | | 2 | lyt | lyt | +------+-------+--------+ 2 rows in set (0.00 sec)
可以看到,我在chat_room數(shù)據(jù)庫中有user_message這張表,我們現(xiàn)在要做的就是讀出這張表里的數(shù)據(jù).
直接上代碼
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<mysql/mysql.h> int main(void) { char *sql; sql="SELECT * FROM user_message;"; int res;//執(zhí)行sql語句后的返回標(biāo)志 MYSQL_RES *res_ptr;//指向查詢結(jié)果的指針 MYSQL_FIELD *field;//字段結(jié)構(gòu)指針 MYSQL_ROW result_row;//按行返回查詢信息 int row,column;//查詢返回的行數(shù)和列數(shù) MYSQL *conn;//一個(gè)數(shù)據(jù)庫鏈接指針 int i,j; //初始化連接句柄 conn = mysql_init(NULL); if(conn == NULL) { //如果返回NULL說明初始化失敗 printf("mysql_init failed!\n"); return EXIT_FAILURE; } //進(jìn)行實(shí)際連接 //參數(shù) conn連接句柄,host mysql所在的主機(jī)或地址,user用戶名,passwd密碼,database_name數(shù)據(jù)庫名,后面的都是默認(rèn) conn = mysql_real_connect(conn,"localhost","lyt","","chat_room",0,NULL,0); if (conn) { printf("Connection success!\n"); } else { printf("Connection failed!\n"); } mysql_query(conn,"set names gbk");//防止亂碼。設(shè)置和數(shù)據(jù)庫的編碼一致就不會(huì)亂碼 res = mysql_query(conn,sql);//正確返回0 if(res) { perror("my_query"); mysql_close(conn); exit(0); } else{ //把查詢結(jié)果給res_ptr res_ptr = mysql_store_result(conn); //如果結(jié)果不為空,則輸出 if(res_ptr) { column = mysql_num_fields(res_ptr); row = mysql_num_rows(res_ptr); printf("查到%d行\(zhòng)n",row); //輸出結(jié)果的字段名 for(i = 0;field = mysql_fetch_field(res_ptr);i++) { printf("%10s",field->name); } puts(""); //按行輸出結(jié)果 for(i = 1;i < row+1;i++){ result_row = mysql_fetch_row(res_ptr); for(j = 0;j< column;j++) { printf("%10s",result_row[j]); } puts(""); } } } //退出前關(guān)閉連接 mysql_close(conn); return 0; }
結(jié)果
gcc -o mysql a.c -L/usr/lib/mysql -lmysqlclient ./mysql Connection success! 查到2行 ID name passwd 1 linux linux 2 lyt lyt
注釋寫的相當(dāng)清楚,有什么不清楚的可以給我留言,大家一塊學(xué)習(xí)!
相關(guān)文章
Linux netfilter/iptables知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是關(guān)于Linux netfilter/iptables知識(shí)點(diǎn)詳解,有興趣的朋友們可以參考下。2020-03-03CentOS8出現(xiàn)-bash:亂碼問題及解決方法
這篇文章主要介紹了CentOS8出現(xiàn)-bash:亂碼問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04如何在 CentOS/RHEL 系統(tǒng)中使用帶 VLAN 標(biāo)記的以太網(wǎng)卡
這篇文章主要介紹了如何在 CentOS/RHEL 系統(tǒng)中使用帶 VLAN 標(biāo)記的以太網(wǎng)卡,有對(duì)這方面感興趣的同學(xué),可以跟隨小編一起來研究學(xué)習(xí)下吧2020-12-12Linux文件服務(wù)器實(shí)戰(zhàn)詳解(匿名用戶)
這篇文章主要介紹了Linux文件服務(wù)器實(shí)戰(zhàn)(匿名用戶),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)
這篇文章主要介紹了CentOS7部署Flask(Apache、mod_wsgi、Python36、venv),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01詳解apache編譯安裝httpd-2.4.54及三種風(fēng)格的init程序特點(diǎn)和區(qū)別
這篇文章主要介紹了apache編譯安裝httpd-2.4.54以及三種風(fēng)格的init程序特點(diǎn)和區(qū)別?,通過編譯安裝httpd來深入理解源碼包安裝(httpd-2.4.54),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07