您的位置:首页 > 数据库

SQL数据插入字符串时转义函数

2015-04-22 17:27 323 查看
函数一:

std::string CheckString(std::string& strSource)
{
std::string strOldValue = "'";
std::string strNewValue = "''";
for(std::string::size_type pos(0); pos != std::string::npos; pos += strNewValue.length())
{
if ((pos = strSource.find(strOldValue, pos)) != std::string::npos)
{
strSource.replace(pos, strOldValue.length(), strNewValue);
}
else
{
break;
}
}
return strSource;
}


方法二:

std::string& replace_all_distinct(std::string& str,const std::string& old_value,const std::string& new_value)
{
for(std::string::size_type pos(0); pos!=std::string::npos; pos+=new_value.length())
{
if((pos=str.find(old_value,pos)) != std::string::npos)
str.replace(pos,old_value.length(),new_value);
else
{
break;
}
}
return str;
}


使用:

例如:

replace_all_distinct("sub'ject","'","''");
CheckString("subj'e'ct");


字符串中有单引号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: