您好,登錄后才能下訂單哦!
小編給大家分享一下mysql+c語言+API如何訪問數據庫,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
#include <winsock2.h> #include "CMySQL.h" #include "stdio.h" #define MYSQL_PORT 3306 void process_result_set(MYSQL mysql, MYSQL_RES *result); void exit(MYSQL mydata,char *ep); int main(int argc, char * argv[]) { MYSQL mydata; Database_Param p; char queryName[600]; MYSQL_RES *result=NULL; int status =0; //初始化 strcpy(p.host,"127.0.0.1"); strcpy(p.user,"root"); strcpy(p.password,"root"); strcpy(p.db,"im"); p.port = MYSQL_PORT; //初始化數據結構 if(mysql_init(&mydata) == NULL) { printf("init mysql data stauct fail\n"); system("pause"); return -1; } //連接 if(argc == 1) { if(NULL == _real_connect(&mydata,p.host,p.user,p.password, p.db,p.port,NULL,CLIENT_MULTI_STATEMENTS)) { printf("connect database fail,%s\n",mysql_error(&mydata)); system("pause"); return -1; } } else { printf("run parameter error\n"); system("pause"); return -1; } //首先設置字符集 strcpy(queryName,"set names gbkd"); if(mysql_query(&mydata,"set names gbk")!=0) { exit(mydata,"set names fail"); return -1; } //查詢數據(支持單個字段和多個字段,輸出排版) strcpy(queryName,"show databases"); if(mysql_query(&mydata,queryName) != 0) { exit(mydata,"execute sql syntax fail"); return -1; } //取得查詢結果 result= mysql_store_result(&mydata); process_result_set(mydata,result); //處理多條插入語句,在mysql_real_connect(......)函數里的最后一個參數更改為:CLIENT_MULTI_STATEMENTS strcpy(queryName,"insert into fangl.admin(username,password) values('fangl1','fangl1'),('fangl2','fangl2'),('fangl3','fangl3');\ insert into fangl.admin(username,password) values('fl1','fl1'),('fl2','fl2'),('fl3','fl3');\ insert into fangl.admin(username,password) values('fal1','fal1'),('fal2','fal2'),('fal3','fal3');\ select * from fangl.admin"); if(mysql_query(&mydata,queryName)!=0) { exit(mydata,"insert values fail"); return -1; } do { /* did current statement return data? */ result = mysql_store_result(&mydata); if (result) { /* yes; process rows and free the result set */ process_result_set(mydata, result); mysql_free_result(result); } else /* no result set or error */ { if (mysql_field_count(&mydata) == 0) { printf("%lld rows affected\n", mysql_affected_rows(&mydata)); } else /* some error occurred */ { printf("Could not retrieve result set\n"); break; } } /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */ if ((status = mysql_next_result(&mydata)) > 0) printf("Could not execute statement\n"); } while (status == 0); //刪除表中的數據 strcpy(queryName,"delete from fangl.admin where username='fl1'"); if(mysql_query(&mydata,queryName) != 0) { exit(mydata,"execute sql syntax fail"); return -1; } //查詢數據(支持單個字段和多個字段,輸出排版) strcpy(queryName,"select * from fangl.admin"); if(mysql_query(&mydata,queryName) != 0) { return -1; } //取得查詢結果 result= mysql_store_result(&mydata); process_result_set(mydata,result); //釋放結果 if(result != NULL) { mysql_free_result(result); } mysql_close(&mydata); system("pause"); return 1; } void process_result_set(MYSQL mydata, MYSQL_RES *result) { int rowcount = mysql_num_rows(result); //查詢結果有多少行 //取得各字段名 MYSQL_FIELD *fields = NULL; for(int i=0;fields=mysql_fetch_field(result);i++) { printf("%s ",fields->name); } printf("\n"); //依次讀取各條記錄 MYSQL_ROW currow = NULL; while((currow = mysql_fetch_row(result)) != NULL) { for(int i = 0; i < mysql_num_fields(result); ++i) { printf("%s ",currow[i]?currow[i]:"NULL"); } printf("\n"); } } void exit(MYSQL mydata,char *ep) { mysql_close(&mydata); printf("%s,%s\n",ep,mysql_error(&mydata)); system("pause"); }
以上是“mysql+c語言+API如何訪問數據庫”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。