当前位置: 技术问答>linux和unix
linux下如何用C脚本连接访问MYSQL
来源: 互联网 发布时间:2015-09-25
本文导语: linux下如何用C脚本连接访问MYSQL | MYSQL提供了接口API,可以在WIN平台和Linux平台下使用,装好后MYSQL库在/usr/lib下,头文件在/usr/include下,你可以去查看!直接调用就可以访问了,例如调用mysql_re...
linux下如何用C脚本连接访问MYSQL
|
MYSQL提供了接口API,可以在WIN平台和Linux平台下使用,装好后MYSQL库在/usr/lib下,头文件在/usr/include下,你可以去查看!直接调用就可以访问了,例如调用mysql_real_connect连接,mysql_close关闭等,若有不明白的你可以联系我!QQ:29304544
|
MYSQL mysql,mysql2;
MYSQL_RES *res;
MYSQL_ROW row;
assert(mysql_init(&mysql)!=NULL);
if((mysql_real_connect_yyy(&mysql,NULL,"root","guchengxian",db,0,NULL,0)==NULL))
{ //fprintf(stderr,"error on connect: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
assert(mysql_init(&mysql2)!=NULL);
if((mysql_real_connect_yyy(&mysql2,NULL,"root","guchengxian",db,0,NULL,0)==NULL))
{ //fprintf(stderr,"error on connect: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
sqllen=sprintf(sqlstr,"select %s from %s where %s",fielname,tab,key_str);
#ifdef DEBUG
prog_debug("In the funtion of delete detail information!
follow is sql str",'Y');
prog_debug(sqlstr,'Y');
#endif
if(mysql_real_query(&mysql,sqlstr,sqllen)!=0)
{ //fprintf(stderr,"error on query: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
if((res=mysql_store_result(&mysql))==NULL)
{ fprintf(stderr,"error on store: %sn",mysql_error(&mysql));
exit(EXIT_FAILURE);
}
if((row=mysql_fetch_row(res))!=NULL)
{ mysql_free_result(res);
}
mysql_close(&mysql);
mysql_close(&mysql2);
MYSQL_RES *res;
MYSQL_ROW row;
assert(mysql_init(&mysql)!=NULL);
if((mysql_real_connect_yyy(&mysql,NULL,"root","guchengxian",db,0,NULL,0)==NULL))
{ //fprintf(stderr,"error on connect: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
assert(mysql_init(&mysql2)!=NULL);
if((mysql_real_connect_yyy(&mysql2,NULL,"root","guchengxian",db,0,NULL,0)==NULL))
{ //fprintf(stderr,"error on connect: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
sqllen=sprintf(sqlstr,"select %s from %s where %s",fielname,tab,key_str);
#ifdef DEBUG
prog_debug("In the funtion of delete detail information!
follow is sql str",'Y');
prog_debug(sqlstr,'Y');
#endif
if(mysql_real_query(&mysql,sqlstr,sqllen)!=0)
{ //fprintf(stderr,"error on query: %sn",mysql_error(&mysql_lg));
exit(EXIT_FAILURE);
}
if((res=mysql_store_result(&mysql))==NULL)
{ fprintf(stderr,"error on store: %sn",mysql_error(&mysql));
exit(EXIT_FAILURE);
}
if((row=mysql_fetch_row(res))!=NULL)
{ mysql_free_result(res);
}
mysql_close(&mysql);
mysql_close(&mysql2);
|
最好的方法是去mysql的网站上,(忘记地址了,网上应该很好搜到,是关于mysql开发的),里面提供面向c/c++的全部函数,还有例子,非常不错