注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

zjc的博客

技术积累

 
 
 

日志

 
 

MYSQL数据库API  

2010-08-09 15:10:34|  分类: c/c++语言 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1.c/c++与mysql数据库api
1)连接
bool GS_cMysql::GS_Mysql_Conn(void)
{
char value = 1;
//初始化连接
if (0 == read_config())
{
return false;
}
else
{
if(mysql_init(&my_conns)==NULL)
{
cout<<"---GS_cMysql.GS_Mysql_Conn---:Initalization failed"<<mysql_error(&my_conns)<<endl;
WriteEvent("Initalization failed", "Mysql error");
return false;
}
//自动重连接配置
mysql_options(&my_conns, MYSQL_OPT_RECONNECT, (char *)&value);
cout<<"---GS_cMysql.GS_Mysql_Conn---:!!!!!!!Connecting......!!!!!!!!"<<endl;
if(mysql_real_connect(&my_conns,mysql_server_ip.c_str(),u_name.c_str(),u_passwd.c_str(),database.c_str(),0,NULL,CLIENT_MULTI_STATEMENTS)== NULL)
{
cout<<"---GS_cMysql.GS_Mysql_Conn---:Connection failed:"<<mysql_error(&my_conns)<<endl;
WriteEvent("Connection failed", "Mysql error");
return false;
}
else
{
cout<<"---GS_cMysql.GS_Mysql_Conn---:MYSQL Connection success\n"<<endl;
if (!mysql_set_character_set(&my_conns, "utf8")) 
{
   printf("---GS_cMysql.GS_Mysql_Conn---:New client character set: %s\n", mysql_character_set_name(&my_conns));
}
else
{
cout<<"---GS_cMysql.GS_Mysql_Conn---:character set error!!"<<endl;
}
}
}
return true;
}
2)查询
bool GS_cMysql::call_sqls(Gdeal_buff_S * gd,Ret_buff_S * ret) { MYSQL_RES *res_ptr; MYSQL_ROW sqlrow; int i,lCount=0; //GS_Mysql_Conn(); mysql_next_result(&Mysqls->my_conns); if (0 == mysql_ping(&Mysqls->my_conns)) { mysql_refresh(&Mysqls->my_conns,REFRESH_TABLES|REFRESH_HOSTS); i= mysql_real_query(&Mysqls->my_conns,gd->sqlins,(unsigned int)strlen(gd->sqlins)); res_ptr = mysql_store_result(&Mysqls->my_conns); if (i !=0) { cout<<"---GS_cMysql.call_sqls---:Error exec res_ptr: "<<mysql_error(&Mysqls->my_conns)<<endl; if (1 == debugmode) { cout<<"---GS_cMysql.call_sqls---:exec sql is "<<gd->sqlins<<endl; } if (res_ptr) mysql_free_result(res_ptr); return RETFLAG_ERROR; } else { if (1 == debugmode) { cout<<"---GS_cMysql.call_sqls---:exec sql is "<<gd->sqlins<<endl; } } ret->err_flag = RETFLAG_OK; i = mysql_num_rows(res_ptr); if (1 == debugmode) { cout<<"---GS_cMysql.call_sqls---:get rows is "<<i<<" and retcnt_flag_s is "<<gd->retcnt_flag_s<<endl; } if (i >= 1) { for( lCount = 0 ; lCount < i; lCount ++ ) { sqlrow = mysql_fetch_row(res_ptr); switch (gd->retcnt_flag_s) { case 0: break; case 1: if('\0' == sqlrow[0]) { strcpy(ret->db_ret_s[lCount].values," "); } else { strncpy(ret->db_ret_s[lCount].values,sqlrow[0],61); } if (1 == debugmode) { cout<<"---GS_cMysql.call_sqls---:1result "<<lCount<<" is "<<ret->db_ret_s[lCount].values<<endl; } break; case 2: strncpy(ret->db_ret_s[lCount].values,sqlrow[0],61); strncpy(ret->db_ret_s[lCount].val2,sqlrow[1],61); if (1 == debugmode) { cout<<"---GS_cMysql.call_sqls---:2result "<<lCount<<" is "<<ret->db_ret_s[lCount].values<<endl; } break; } } ret->ret_cnt_s = i; ret->err_flag = RETFLAG_OK; } else { //没有取到结果 ret->ret_cnt_s = i; } if (res_ptr) mysql_free_result(res_ptr); } else { cout<<"---GS_cMysql.call_sqls---:mysql connect handle error"<<mysql_error(&Mysqls->my_conns)<<endl; //建议重新连接 GS_Mysql_ReConn(); ret->err_flag = RETFLAG_ERROR; } return RETFLAG_OK; }
3)修改
bool GS_cMysql::call_sql_change(Gdeal_buff_S * gd,Ret_buff_S * ret) { MYSQL_RES *res_ptrs; int i; mysql_next_result(&Mysqls->my_conns); if (0 == mysql_ping(&Mysqls->my_conns)) { mysql_refresh(&Mysqls->my_conns,REFRESH_TABLES|REFRESH_HOSTS); i= mysql_real_query(&Mysqls->my_conns,gd->sqlinsc,(unsigned int)strlen(gd->sqlinsc)); if (i !=0) { cout<<"---GS_cMysql.call_sql_change---:Error exec res_ptr: "<<mysql_error(&Mysqls->my_conns)<<endl; if (1 == debugmode) { cout<<"---GS_cMysql.call_sql_change---:exec sql is "<<gd->sqlinsc<<endl; } ret->err_flag = RETFLAG_ERROR; return RETFLAG_ERROR; } else { if (1 == debugmode) { cout<<"---GS_cMysql.call_sql_change---:exec sql is "<<gd->sqlinsc<<endl; } } ret->err_flag = RETFLAG_OK;
//防止命令异步错误 do { res_ptrs = mysql_store_result( &Mysqls->my_conns ); if (res_ptrs) mysql_free_result(res_ptrs); }while( !mysql_next_result( &Mysqls->my_conns ) ); } else { cout<<"---GS_cMysql.call_sql_long---:mysql cnnect handle error"<<mysql_error(&Mysqls->my_conns)<<endl; //建议重新连接 GS_Mysql_ReConn(); ret->err_flag = RETFLAG_ERROR; } return RETFLAG_OK; }
4)调用存储过程
bool GS_cMysql::call_procedures(Gdeal_buff_S * gd,Ret_buff_S * ret) { MYSQL_RES *res_ptr; MYSQL_RES *res_ptrs; MYSQL_ROW sqlrow; int i,j; if ('\0' == gd->sqlin[0]) { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:sqlin is null:"<<gd->sqlin<<endl; } ret->err_flag = RETFLAG_ERROR; return RETFLAG_ERROR; } if ('\0' == gd->sqlout) { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:sqlout is null"<<endl; } ret->err_flag = RETFLAG_ERROR; return RETFLAG_ERROR; } if (0 == mysql_ping(&Mysqls->my_conns)) { //需要测试连接,异常处理 mysql_refresh(&Mysqls->my_conns,REFRESH_TABLES|REFRESH_HOSTS); i = mysql_real_query(&Mysqls->my_conns,gd->sqlin,gd->sqlin_len); if (i != 0) { cout<<"---GS_cMysql.call_procedures---:Error exec res_ptr: "<<mysql_error(&Mysqls->my_conns)<<endl; if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:Error exec sql is "<<gd->sqlin<<endl; } ret->err_flag = RETFLAG_ERROR; return RETFLAG_ERROR; } else { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:exec procedure :"<<gd->sqlin<<endl; } } do { res_ptrs = mysql_store_result( &Mysqls->my_conns ); if (res_ptrs) mysql_free_result(res_ptrs); }while( !mysql_next_result( &Mysqls->my_conns ) ); i = mysql_query(&Mysqls->my_conns, gd->sqlout); if (i != 0) { cout<<"---GS_cMysql.call_procedures---:Error exec res_ptr: "<<mysql_error(&Mysqls->my_conns)<<endl; if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:Error exec sql is "<<gd->sqlout<<endl; } ret->err_flag = RETFLAG_ERROR; return RETFLAG_ERROR; } else { } res_ptr = mysql_store_result(&Mysqls->my_conns); if (res_ptr) { ret->err_flag = RETFLAG_OK; i = mysql_num_rows(res_ptr); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---: 2.######Result rows is "<<i<<"#####"<<endl; } j = mysql_field_count(&Mysqls->my_conns); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---: 2. ######Result count is "<<j<<"#####"<<endl; } if (i >=1) { while((sqlrow=mysql_fetch_row(res_ptr))) { switch(gd->retcnt_flag_p) { case 1: if ('\0' == sqlrow[0]) { ret->err_flag = RETFLAG_ERROR; } else { strncpy(ret->db_ret_p[0].values,sqlrow[0],strlen(sqlrow[0])); ret->err_flag = atoi(sqlrow[0]); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:1 flag is "<<ret->db_ret_p[0].values<<endl; } } break; case 2: if ('\0' == sqlrow[1]) { ret->err_flag = RETFLAG_ERROR; } else { strncpy(ret->db_ret_p[1].values,sqlrow[1],strlen(sqlrow[1])); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:2 flag is "<<ret->db_ret_p[1].values<<endl; } ret->err_flag = atoi(sqlrow[1]); } if (atoi(sqlrow[1]) == 1) { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:2 result is "<<ret->db_ret_p[0].values<<endl; } if ('\0' == sqlrow[0]) { strncpy(ret->db_ret_p[0].values," ",1); } else { strncpy(ret->db_ret_p[0].values,sqlrow[0],strlen(sqlrow[0])); } } break; case 3: if ('\0' == sqlrow[2]) { ret->err_flag = RETFLAG_ERROR; } else { strncpy(ret->db_ret_p[2].values,sqlrow[2],strlen(sqlrow[2])); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:3 flag is "<<atoi(sqlrow[2])<<endl; } ret->err_flag = atoi(sqlrow[2]); } if (atoi(sqlrow[2]) == 1) { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:3 result is"<<sqlrow[0]<<sqlrow[1]<<sqlrow[2]<<endl; } //sprintf(rets ,"%s|%s|%s",sqlrow[0],sqlrow[1],sqlrow[2]); if ('\0' == sqlrow[0]) { strncpy(ret->db_ret_p[0].values," ",1); } else { strncpy(ret->db_ret_p[0].values,sqlrow[0],strlen(sqlrow[0])); } if ('\0' == sqlrow[1]) { strncpy(ret->db_ret_p[1].values," ",1); } else { strncpy(ret->db_ret_p[1].values,sqlrow[1],strlen(sqlrow[1])); } } break; case 4: if ('\0' == sqlrow[3]) { ret->err_flag = RETFLAG_ERROR; } else { strncpy(ret->db_ret_p[3].values,sqlrow[3],strlen(sqlrow[3])); if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:4 flag is "<<atoi(sqlrow[3])<<endl; } ret->err_flag = atoi(sqlrow[3]); } if ((atoi(sqlrow[3]) == 1) || (atoi(sqlrow[3]) == 2)) { if ('\0' == sqlrow[0]) { strncpy(ret->db_ret_p[0].values," ",1); } else { strncpy(ret->db_ret_p[0].values,sqlrow[0],strlen(sqlrow[0])); } if ('\0' == sqlrow[1]) { strncpy(ret->db_ret_p[1].values," ",1); } else { strncpy(ret->db_ret_p[1].values,sqlrow[1],strlen(sqlrow[1])); } if ('\0' == sqlrow[2]) { strncpy(ret->db_ret_p[2].values," ",1); } else { strncpy(ret->db_ret_p[2].values,sqlrow[2],strlen(sqlrow[2])); } } break; } } ret->ret_cnt_p = gd->retcnt_flag_p; } } else { if (1 == debugmode) { cout<<"---GS_cMysql.call_procedures---:result NULL :"<<endl; } } mysql_free_result(res_ptr); } else { cout<<"---GS_cMysql.call_procedures---:mysql connect handle error"<<mysql_error(&Mysqls->my_conns)<<endl; //建议重新连接 GS_Mysql_ReConn(); ret->err_flag = RETFLAG_ERROR; } return RETFLAG_OK; }
  评论这张
 
阅读(1334)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018