mysql_init初始化數(shù)據(jù)庫(kù)鏈接–得到一個(gè)鏈接 mysql_real_connect連接數(shù)據(jù)庫(kù)服務(wù)器 執(zhí)行mysql_query查詢–查詢語句是一個(gè)字符串 對(duì)每一行分別進(jìn)行檢索mysql_store_result–結(jié)果存儲(chǔ)在鏈接里面,屬于一次性查詢 從結(jié)果集,獲取表頭信息–mysql_fetch_fields–表頭信息存儲(chǔ)在mysql_field類型的指針指向的內(nèi)存空間 解析表頭–mysql_field_count獲取列數(shù),for循環(huán)逐列解析 mysql_fetch_row從結(jié)果集中一行一行的獲取數(shù)據(jù),在針對(duì)每一行解析每一列 釋放內(nèi)存空間
關(guān)閉鏈接
具體代碼
#include?<stdio.h> #include?<stdlib.h> #include?<string.h> #include?<dlfcn.h> #include?<mysql> int?main() { ????int?????ret?=?0; ????int?????i?=?0; ????MYSQL???mysql; ????MYSQL???*con?=?NULL; ????unsigned?int?fieldnum;?? ????printf("hello....n"); ????con?=?mysql_init(&mysql); ????if?(con?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_init()?err?:%dn",?ret); ????????return?ret; ????} ????con?=?mysql_real_connect(&mysql,?"localhost",?"root",?"123456",?"mydb2",?0,?NULL,?0?); ????if?(con?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_real_connect()?err?:%dn",?ret); ????????return?ret; ????} ????else ????{ ????????printf("func?mysql_real_connect()?okn"); ????} ????//執(zhí)行sql查詢 ????mysql_query(&mysql,?"set?names?utf8");//解決中文亂碼 ????char?*sql?=?"select?*from?employee"; ????ret?=?mysql_query(&mysql,?sql); ????if?(ret?!=?0) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_query()?err?:%dn",?ret); ????????return?ret; ????} ????/* ????//獲取結(jié)果集?一次性獲取 ????MYSQL_RES?*?sqlres?=mysql_store_result(?&mysql); ????if?(sqlres?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_store_result()?err?:%dn",?ret); ????????return?ret; ????} ????*/ ????//對(duì)每一行分別進(jìn)行檢索 ????MYSQL_RES?*?sqlres?=mysql_store_result(&mysql); ????if?(sqlres?==?NULL) ????{ ????????ret?=?mysql_errno(&mysql); ????????printf("func?mysql_store_result()?err?:%dn",?ret); ????????return?ret; ????} ????//從結(jié)果集,獲取表頭信息 ????MYSQL_FIELD?*fields?=?mysql_fetch_fields(sqlres); ????fieldnum?=?mysql_field_count(&mysql); ????for?(i=0;?i<fieldnum><p>以上就是MySQL入門之簡(jiǎn)單數(shù)據(jù)查詢的內(nèi)容,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)(www.php.cn)!</p></fieldnum></mysql></dlfcn.h></string.h></stdlib.h></stdio.h>
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END