小弟对程序中查询语句的''使用不太明确。我执行这样的SQL语句:select zgid from zgxx where xm=××
设了几个变量:
stroption:='=';
strname:=edit1.text;  //edit1.text中放的是希望查询的xm的值
strselect:='xm';
请大家帮我指出下面SQL的错误
  ADOQuery1.Close;
  ADOQuery1.SQL.clear;
  ADOQuery1.SQL.Add('select zgid');
  ADOQuery1.SQL.Add('from zgxx');
  ADOQuery1.SQL.Add('where xm '+''+stroption+''+'strname');
  ADOQuery1.Prepared;
  try
  ADOQuery1.Open;
  except
  ADOQuery1.ExecSQL;

解决方案 »

  1.   

    'where xm '+''+stroption+''+'strname' 这个语句有错误阿!!!
      

  2.   

    ADOQuery1.SQL.Add('select zgid');
      ADOQuery1.SQL.Add('from zgxx');
      ADOQuery1.SQL.Add('where ' + strselect + stroption + '''strname''');
      

  3.   

    ADOQuery1.SQL.Add('select zgid');
      ADOQuery1.SQL.Add('from zgxx');
      ADOQuery1.SQL.Add('where ' + strselect + stroption + '''strname''');应该改为:
    ADOQuery1.SQL.Add('select zgid');
      ADOQuery1.SQL.Add('from zgxx');
      ADOQuery1.SQL.Add('where ' + strselect + stroption + '''+trname+'''');
      

  4.   

    ADOQuery1.SQL.Add('select zgid');
      ADOQuery1.SQL.Add(' from zgxx');
    from 后要加空格
      

  5.   

    'select zqid from zgxx where '+strselect+stroption+'"'+strname+'"'
      

  6.   

    有没有大虾可以解释一下strname的引号是为什么有那么多?
      

  7.   

    strname前后都是 单引,双引,单引,因为再sql中字符串要用单引号,而在pascal中也是这样,所以改为双引就行了
      

  8.   

    不好意思,小弟问一个土土的问题,双引是什么时候用呀?//bow谢谢nerv兄先
      

  9.   

    ADOQuery1.Close;
      ADOQuery1.SQL.clear;
      ADOQuery1.SQL.Add('select zgid');
      ADOQuery1.SQL.Add(' from zgxx');
      ADOQuery1.SQL.Add(' where xm '+stroption+'''+strname+'''');
      ADOQuery1.Prepared;
      try
      ADOQuery1.Open;
      except
      ADOQuery1.ExecSQL;这样可以做到输出:select zgid from zgxx where xm='(strname的内容)'
      

  10.   

    就是在sql语句中需要用到单引号的时候用双引号代替
      

  11.   

    adoquery.sql.text:='select zgid from zgxx where xm '+stroption+quotedstr(edit1.text);
    adoquery.open
      

  12.   

    我不习惯用ADD,
    还是TEXT好用,就不用clear了 BDE的dataset都不用close,
    ADO好像有点问题
      

  13.   

    '''表示一个单引号;
    F1中Help的解释A character string, also called a string literal or string constant, consists of a quoted string, a control string, or a combination of quoted and control strings. Separators can occur only within quoted strings.
    A quoted string is a sequence of up to 255 characters from the extended ASCII character set, written on one line and enclosed by apostrophes. A quoted string with nothing between the apostrophes is a null string. Two sequential apostrophes in a quoted string denote a single character, namely an apostrophe. For example,'BORLAND'           { BORLAND }'You''ll see'       { You'll see }
    ''''                { ' }
    ''                  { null string }
    ' '                 { a space }A control string is a sequence of one or more control characters, each of which consists of the # symbol followed by an unsigned integer constant from 0 to 255 (decimal or hexadecimal) and denotes the corresponding ASCII character. The control string#89#111#117is equivalent to the quoted string'You'You can combine quoted strings with control strings to form larger character strings. For example, you could use'Line 1'#13#10'Line 2'to put a carriage-return杔ine-feed between 揕ine 1?and 揕ine 2? However, you cannot concatenate two quoted strings in this way, since a pair of sequential apostrophes is interpreted as a single character. (To concatenate quoted strings, use the + operator or simply combine them into a single quoted string.)
    A character string抯 length is the number of characters in the string. A character string of any length is compatible with any string type and with the PChar type. A character string of length 1 is compatible with any character type, and, when extended syntax is enabled (
    {$X+}), a nonempty character string of length n is compatible with zero-based arrays and packed arrays of n characters. For more information about string types, see String types.