procedure TForm1.Button1Click(Sender: TObject);
var
x:string;
begin
x:=edit1.Text;
query1.Close;
query1.SQL.Clear;
query1.sql.Add('select * from people where 姓名=x');
query1.open;
dbgrid1.datasource:=datasource1;
dbgrid1.refresh;
end;
end.
其中 people 为数据表名 姓名为字段名称
系统提示错误信息 x为无效字段

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    x:string;
    begin
    x:=edit1.Text;
    query1.Close;
    query1.SQL.Clear;
    query1.sql.Add('select * from people where 姓名='+x);
    query1.open;
    dbgrid1.datasource:=datasource1;
    dbgrid1.refresh;
    end;
    end.
    其中 people
      

  2.   

    query1.sql.Add('select * from people where 姓名='+x);////只这句改正一下就可以了
      

  3.   

    ^_^怎么还是这个;抢分了;^_^;
    1、query1.sql.Add('select * from people where 姓名='''+x+'''');
    2、query1.sql.Add('select * from people where 姓名='+quotedstr(X));
    3、query1.sql.Add('select * from people where 姓名 = :1');
       parameters.parambyname('1').value := X;
      

  4.   

    是的 就是你的引号的用法不是很熟练
    query1.sql.Add(''''+x+'''');
    这样其实 sql 语句是select * from people where 姓名='参数的值'
      

  5.   

    不好意思了 写错了
    query1.sql.Add('select * from people where 姓名='''+x+'''');
    这样其实 sql 语句是select * from people where 姓名='参数的值'
      

  6.   

    X可以不要,直接写
    query1.sql.add('select*form people where 姓名='''+edit1.text+''');
      

  7.   

    query1.sql.Add('select * from people where 姓名=''x''');x为你要查询的姓名。
      

  8.   

    query1.sql.Add('select * from people where 姓名='+#39+'x'+#39);
      

  9.   

    这个问题我也遇到过。
      这是一个动态SQL语句问题,当SQL语句中还要用到变量的话,可以这样处理:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    x:string;
    begin
    x:=edit1.Text;
    query1.Close;
    query1.SQL.Clear;
    query1.sql.Add('select * from people where 姓名=:N');  //这里‘N’可以随便取,但前面必须加上冒号;
    Parameters.ParamByName('N').Value:=x;
    query1.open;
    dbgrid1.datasource:=datasource1;
    dbgrid1.refresh;
    end;
    end.你试试看!