在日常的数据库软件开发中,有一些个特殊字符挺烦人。
比如 ' (单引号),如果有这个字符内就会有问题。在构建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;