mysql+c语言+API如何访问数据库
小编给大家分享一下MySQL+c语言+API如何访问数据库,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
按需网站策划可以根据自己的需求进行定制,成都做网站、网站制作构思过程中功能建设理应排到主要部位公司成都做网站、网站制作的运用实际效果公司网站制作网站建立与制做的实际意义
#include#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如何访问数据库”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
当前标题:mysql+c语言+API如何访问数据库
链接URL:http://scjbc.cn/article/joijos.html