当前位置:C++技术网 > 资讯 > mysql编程中空字符的处理方法

mysql编程中空字符的处理方法

更新时间:2017-03-01 15:46:10浏览次数:1+次

    mysql编程中空字符的处理查了半天也没有查到。是太简单了还是太复杂了呢?
    在mysql编程中,我们查询了之后,然后用mysql_fetch_row来一行行的从数据集中取数据,然后一个个的数据取出来进行处理。
    mysql里面的数据返回都是以字符串的形式。然后我就统一将他们转换为string。转换的代码如下:
string str = string(row[i]);
    当数据表中出现NULL的字段时,崩溃了,提示:
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
    这个表明是string在构造的时候遇到了null。这里提示很明显的。
    null的值实际就是0。所以你可以这样来检测:
string str;
if(row[i]==NULL) str="";
else str=string(row[i]);

    也可以这样:
string str;
if(row[i]==0) str="";
else str=string(row[i]);

    还可以这样:
string str = (row[i]==0)?"":row[i];