本中所使用的table:
MySQL中的錯誤處理函數
unsigned int mysql_errno(MYSQL *mysql)
const char *mysql_error(MYSQL *mysql)
說明:一個函數是返回錯誤號,一個返回錯誤信息,兩者都以MYSQL*為參數,很直觀。
第一步:進行查詢操作
用到的函數:
int mysql_query(MYSQL *mysql, const char *stmt_str)
int mysql_real_query(MYSQL *mysql, const char *stmt_str, unsigned longlength)
返回值:成功則返回0,失敗返回非零,如果失敗的話,可以利用錯誤處理函數來獲得錯誤信息。
這兩個函數的區別:當查詢語句使用的是二進制數據時,只能用后者。
第二步:保存查詢結果(僅select語句具有查詢結果,update、delete、insert是沒有查詢結果的)
用到的函數:
MYSQL_RES *mysql_store_result(MYSQL *mysql)
MYSQL_RES *mysql_use_result(MYSQL *mysql)
返回值:如果執行成功,返回一個非空的MYSQL_RES指針,否則,返回NULL。注意,只要執行select語句成功,就算沒有與該select語句匹配的表項,該函數的返回值也不應為NULL。
兩個函數的區別:mysql_store_result將整個result set放進client中存儲,假若select的查詢結果有一百個表項,則調用mysql_store_result會將這一百個表項全都存在了內存中,而mysql_use_result僅僅從result set中取出一個表項存在內存中,使用mysql_fetch_row()函數時才從server中的result set中取出下一個表項。因此mysql_use_result占用的內存更少執行速度也更快。但如果你需要在client端對result set的數據進行一些處理的話,那還是用mysql_store_set吧。
總結:mysql_store_result將select語句在server中的執行結果全都存到了client的內存中,而mysql_use_result僅當需要用到數據時才從server中取出下一條數據存到client的內存中。
注意:結果用完之后,要用mysql_free_result()釋放內存。該函數的原型為 void mysql_free_result(MYSQL_RES *result)
第三步:遍歷結果
遍歷結果可分為兩個步驟:首先取出result set中的一行數據,然后再遍歷該行數據中的每列數據。
遍歷行時用到的函數:
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
說明:它以上一步中的查詢結果為參數,返回一個MYSQL_ROW結構體,這個結構體的內容為result set中的一行數據,可以理解為一個元素為字符指針的數組(char* row[elems]),當result set中的所有數據遍歷完畢,該函數返回NULL。那么怎樣使用這個結構體呢?首先得通過下面的函數(mysql_num_fields() / mysql_field_count())得到result set中的列數,然后從index從0開始,以(列數 - 1)結束,依此遍歷就可以了。以col代指當前列,若當前列的值為NULL,MYSQL_ROW[col]的值也為NULL。
使用示例:
1 MYSQL_ROW row; 2 unsigned int cols = mysql_field_count(conn_ptr); 3 while ((row = mysql_fetch_row(res_ptr)) != NULL) 4 { 5 for (unsigned int i = 0; i < cols; ++i) 6 { 7 printf("%s ", row[i]); 8 } 9 printf("\n"); 10 }
輸出結果如下:
其實正常情況下應該對row[i]是否為NULL進行判斷的,這里讓我有點疑惑,為何row[i]在為空的情況下,也行正常printf出結果,而不產生段錯誤。
1 MYSQL_ROW row; 2 unsigned int cols = mysql_field_count(conn_ptr); 3 while((row = mysql_fetch_row(res_ptr)) != NULL) 4 { 5 for(unsigned int i = 0;i < cols;++i) 6 { 7 if(row[i] == NULL) 8 printf("i am null"); 9 else 10 printf("%s ",row[i]); 11 } 12 printf("\n"); 13 }
結果:
遍歷列時用到的函數:
unsigned int mysql_num_fields(MYSQL_RES *result)
unsigned int mysql_field_count(MYSQL *mysql)
MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result)
對於前面兩個函數,它們的參數不同,但是效果卻是相同的,都是返回當前查詢的result set中的列數。
通過第三個函數可以得到列的屬性(包括列的name,列的type等信息),它的用法與mysql_fetch_col()差不多,也需要用while不停地循環。當返回值為空時,就代表沒有下一列了。
使用示例:
1 MYSQL_RES* res_ptr = mysql_store_result(conn_ptr); 2 if(res_ptr) 3 { 4 printf("use result successful\n"); 5 6 MYSQL_FIELD* field; 7 while((field = mysql_fetch_field(res_ptr))) 8 { 9 printf("%s ",field->name); 10 } 11 printf("\n"); 12 13 }
結果:
全部代碼
1 #include <mysql/mysql.h> 2 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 int main() 7 { 8 //MYSQL* mysql_init(MYSQL *mysql) 9 MYSQL* conn_ptr = mysql_init(NULL); 10 if(conn_ptr == NULL) 11 { 12 fprintf(stderr,"mysql_init() failed\n"); 13 return -1; 14 } 15 //MYSQL* mysql_real_connect(MYSQL *mysql, const char *host, 16 //const char *user,const char *passwd, const char *db, 17 //unsigned int port, const char *unix_socket, unsigned long clientflag) 18 mysql_real_connect(conn_ptr,"localhost", 19 "rick","secretpassword","rick", 20 0,NULL,0); 21 if(conn_ptr) 22 { 23 printf("connect successfully\n"); 24 int query_ret = mysql_query(conn_ptr,"SELECT * FROM children;"); 25 if(query_ret == 0) 26 { 27 printf("query successful\n"); 28 29 MYSQL_RES* res_ptr = mysql_store_result(conn_ptr); 30 if(res_ptr) 31 { 32 printf("use result successful\n"); 33 34 MYSQL_FIELD* field; 35 while((field = mysql_fetch_field(res_ptr))) 36 { 37 printf("%s ",field->name); 38 } 39 printf("\n"); 40 41 MYSQL_ROW row; 42 unsigned int cols = mysql_field_count(conn_ptr); 43 while((row = mysql_fetch_row(res_ptr)) != NULL) 44 { 45 for(unsigned int i = 0;i < cols;++i) 46 { 47 printf("%s ",row[i]); 48 } 49 printf("\n"); 50 } 51 } 52 else 53 { 54 fprintf(stderr,"use result failed %d : %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr)); 55 } 56 mysql_free_result(res_ptr); 57 } 58 else 59 { 60 fprintf(stderr,"query failed %d : %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr)); 61 } 62 63 } 64 else 65 { 66 printf("connect failed\n"); 67 } 68 //void mysql_close(MYSQL *sock) 69 mysql_close(conn_ptr); 70 return 0; 71 }