Delphi使用动态sql语句时出现错误

来源:百度知道 编辑:UC知道 时间:2024/06/22 00:32:49
procedure TForm1.Button3Click(Sender: TObject);
begin
Name:=Edit1.Text;
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('select * from animals.dbf');
Query1.SQL.Add('where name=:name');
Query1.ParamByName('name').AsString:=Name;

if Query1.RecordCount=0 then
ShowMessage('没有这条记录')
else begin
Query1.Close;
Query1.SQL.Clear;

Query1.SQL.Add('update animals.dbf');
query1.SQL.Add('set name="New Name"');
Query1.SQL.Add('where name=:name');
Query1.ParamByName('name').AsString:=Name;
Query1.ExecSQL;

Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('select * from animals.dbf');
Query1.Open;
end;
end;

end.
上面全局变量忘写了
var
Name:string;

错误提示数据集没有打开,你改为下面的代码看看
procedure TForm1.Button3Click(Sender: TObject);
begin
Name:=Edit1.Text;
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('select * from animals.dbf');
Query1.SQL.Add('where name=:name');
Query1.ParamByName('name').AsString:=Name;

Query1.open;//这里应该添加一句,打开数据集
if Query1.RecordCount=0 then
ShowMessage('没有这条记录')
else begin
Query1.Close;
Query1.SQL.Clear;

Query1.SQL.Add('update animals.dbf');
query1.SQL.Add('set name="New Name"');
Query1.SQL.Add('where name=:name');
Query1.ParamByName('name').AsString:=Name;
Query1.ExecSQL;

Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('select * from animals.dbf');
Query1.Open;
end;
end;

end.

Query1.SQL.Add('select * from animals.dbf');
Query1.SQL.Add('