with AdoQuery1 do
begin
  Close;
  SQL.Clear;
  SQL.Add('select * from T_Custom where CustName='''+CustName+'''');
  Open;
end;
这时delphi会提标有错,因为custname中有单引号,会有错误。
现在CustName这个变量中有一个'号,比如CustName:='sssssss'dseee';
我该如何做呢?用Pos能判断出一个字符串中是否存在单引号?

解决方案 »

  1.   

    showmessage(inttostr(pos(#39,CustName)));
      

  2.   

    Delphi中字符串中的单引号使用双引号来表示,比如'''',就表示一个包含单引号的字符串,你可以据此判断,另外,你在SQL语句中变量有单引号是一个普遍问题,Delphi有一个函数解决这个问题,你的代码可以改为:
    with AdoQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from T_Custom where CustName='+Quotedstr(CustName));
      Open;
    end;Quotedstr函数会自动将字符串中的单引号改为双引号的表示,并且在字符串两边加上双引号,使用很方便。
      

  3.   

    pos()可以的
    不过楼上的方法更好啊var
    str:string;
    begin
    str:='abc''d';
    str:=Quotedstr(str);
    showmessage(str);
    end;str此时的值是'abc''d',自动在两端加了引号!