MySQL C API的一个让我头疼的问题,获得一行记录中包括NULL

时间:2023-06-02 22:33:07

遇到过几次错误,通过gdb来查看错误对战,发现错误居然是atoi调用出错,除非atoi(NULL) 才会报这种错误.说明 row[0]==NULL.

(gdb) bt
#0 0x00007f82c6629132 in ____strtoll_l_internal () from /lib64/libc.so.6
#1 0x00007f82c6625ee0 in atoi () from /lib64/libc.so.6
#2 0x0000000000438c7b in MySQL_Util::select_one (
sql_string=0x7f82c4544190 "select sum(pointcoupon_added) from tb_recharge_records where user_id=214873 and status=0 and method=0 ", get_data=0x7f82c4544598, length=4,
data_type=MYSQL_INT, pmysql=0x7f82c4545210) at MySQL_Util.hpp:177

好无语啊,SQL语句中包括 sum() max() count(),及时没有返回值,也会执行到 这里面:

            MYSQL_RES *res = NULL;
MYSQL_ROW row = NULL;
res=mysql_store_result(pmysql);
if( (row=mysql_fetch_row(res) )!=NULL )
{
// atoi(row[0])
}else{
nReturn=MYSQL_SQL_FAILED;
}
mysql_free_result(res);
row=NULL;

NULL也会返回一行记录,真是够恶心的啊!只能判断 if(row[0]!=NULL) XXX=atoi(row[0]);

或许 atoi重新定义下 就好了:

static inline int mystrlen(char *str){//const
if(str==NULL)
return ;
else
return strlen(str);
}
static inline int myatoi(char * str){
if(str==NULL)
return ;
else
return atoi(str);
}

类似的还有strlen(NULL)也会报错,一起改了.