例如:
procedure TForm1.Button1Click(Sender: TObject);
   var Sql_Str:string;
begin
   Sql_Str:='select a.a,a.b from a where a.b in(';
   Sql_Str:=Sql_Str+'select b.b from b where b.c=1)';
   adoquery1.SQL.Clear;
   adoquery1.SQL.add(Sql_Str);
   adoquery1.Open;
end;
如何在字符串中增加用户自定义的条件???
例如上面:sql_str:=sql_str+'select b.b from b where b.c=1 and b.d=+''''dblookupcombobox1.text+'''' )';
请问上面这样写行吗??我尝试过,通不过啊????
因为上面的语句中字符串包含一对括号,而我的这个用户所定义的条件一定要在括号内,所以一定要这样写,请问改如何解决?????

解决方案 »

  1.   

    sql_str:=sql_str+'select b.b from b where b.c=1 and b.d=‘+
    '''dblookupcombobox1.text'''+' )';
      

  2.   

    try this:
    sql_str:=sql_str+'select b.b from b where b.c=1 and b.d='''+dblookupcombobox1.text+'''' )';
      

  3.   

    sql_str := sql_str + 'select b.b from b where b.c=1 and b.d = ''' + dblookupcombobox1.text + ''')'
      

  4.   

    sql_str:=sql_str+'select b.b from b where b.c=1 and b.d='+QuotedStr(dblookupcombobox1.text)+')'
      

  5.   

    例如上面:sql_str:=sql_str+'select b.b from b where b.c=1 and b.d=+''''dblookupcombobox1.text+'''' )';你的第一个四个引号应该改为三个引号,即sql_str:=sql_str+'select b.b from b where b.c=1 and b.d=+'''dblookupcombobox1.text+'''' )';查delphi的帮助可以知道,delphi中字符串用单引号引起来。那单引号本身如何表示?就是用单引号扩起来的两个单引号表示,所以三个引号和前面的那个单引号共同构成'''',表示一个单引号,后面的四个单引号表示一个单引号,正好一对单引号,将dblookupcombobox1.text得到的结果括起来。
      

  6.   

    为什么不用quotedstr()?非得用那么多的'''
      

  7.   

    //这样写才是好的,给分吧:
    Sql_Str := Format('select a.a,a.b from a  '
    + 'where a.b in(select b.b from b where b.c=1) and b.d= ''%s'''
    , [dblookupcombobox1.text]);
    //(Format还有好多用法可以看看帮助。)