经常因为殊字符的问题导致查询异常

    在日常的数据库软件开发中,有一些个特殊字符挺烦人。

    比如 '  (单引号),如果有这个字符内就会有问题。在构建SQL语句的时间就要特殊处理它。

    为了解决这个问题当然还有其它的方法,在这里我想从根源上解决,就是在保存数据时就自动替换为全角字符

function PUBF_ConvertSpecialChar(const s:string):string;
var
  nlength,i:integer;
  str,ctmp,c1:string;
begin
  nlength:= length(s);
   if  (nlength = 0 ) then    exit;
  str:= '' ;
  ctmp:= s;
  i:= 1 ;
  while  (i <= nlength)  do
  begin
    c1:= ctmp[i];

    case ord(c1[1]) of
//      32..48,58..64,91..96,123..127:
      33..48,58..64,91..96,123..127:  //不包含空格
      begin
        str:= str + #163+ chr(ord(c1[1]) + 128 );
      end
      else
      str:= str + c1;
    end;
    inc(i);
  end;
  result:= str;

end;


function PUBF_ConvertFullWidthChar(const TblName: TDataSet;
  AFn: string): Boolean;
var
  OrgS,DstS:string;
begin
  Result :=True;
  if not TblName.Active then Exit;
  if TblName.STATE in [dsEdit,dsInsert] then TblName.Post;
  if Trim(AFn)='' then Exit;
  try
    OrgS:= TblName.FieldByName(AFn).AsString;
    if Trim(OrgS)='' then Exit;
    DstS:= PUBF_ConvertSpecialChar(OrgS);
    if OrgS = DstS then  Exit;
    MsgBox(Format(MSG_CHAR_Invalid,[OrgS,DstS]));
    with TblName do begin
      if not (STATE in [dsEdit,dsInsert]) then Edit;
      FieldByName(AFn).AsString := DstS ;
    end;
  except
    on e:Exception do begin
      ShowErrorMessage(e.Message);
      Result := False;
    end;
  end;
end;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值