如何在函数内添加查询

How to add query inside a function

我正在尝试创建一个包含整个查询 (CRUD) 的 class 供其他 units/forms 调用和使用。但是后来我遇到了一些错误。 我正在使用 Firebird 作为数据库。

我不知道如何添加查询以在函数内部执行。 当我尝试在函数中输入查询代码时出现错误。

unit AllClass;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
FireDAC.Stan.Intf, FireDAC.Stan.Option,FireDAC.Stan.Error,FireDAC.UI.Intf, 
FireDAC.Phys.Intf, FireDAC.Stan.Def,FireDAC.Stan.Pool, FireDAC.Stan.Async, 
FireDAC.Phys, FireDAC.Phys.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait, 
FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, 
FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;

type
 TForm6 = class(TForm)
 FDConnection1: TFDConnection;
 EveryQuery: TFDQuery;
 procedure login(username,password:String);
 private
  { Private declarations }
 public
  { Public declarations }
end;

var
 Form6: TForm6;

implementation

procedure login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;
end.

但是在按钮触发过程中输入查询代码不会出错

procedure TForm1.LogInClick(Sender: TObject);
begin
if (Username.Text <> '') And (Password.Text <> '') then
begin
loginQuery.SQL.Clear;

//use parameter method
loginQuery.SQL.Text := 'Select Password from MYGUESTS where FIRSTNAME = :theID';
loginQuery.ParamByName('theID').AsString := Username.Text;
loginQuery.Open();
passwords := loginQuery.FieldByName('Password').AsString;
if(passwords = Password.Text) then
begin
  with form3 do
  begin
    Show;
    Username.Clear;
    Password.Clear;

    Form1.Hide;
  end;
end
else
begin
  ShowMessage('Wrong username or password. please re-enter');
end;
end
else
begin
  ShowMessage('Please enter something');
end;
end;

如果您对此问题有任何解决方案或想法,请帮助我。

您的 function login 应该是 function TForm6.Login,因为它被声明为表单本身的方法。您可以通过以 class 形式编写声明(在私有或 public 部分,而不是您目前拥有的部分)并点击 Ctrl 来避免此问题+Shift+C,IDE 将在 implementation 部分生成正确的代码。

您当前的代码:

procedure login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;

更正后的代码:

procedure TForm6.login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;

您的 login() 过程 声明 作为您 TForm6 class 的成员,但您没有 实施 它作为 class 成员。 implementation段中的login过程名前需要加上TForm6.,例如:

unit AllClass;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
  FireDAC.Stan.Intf, FireDAC.Stan.Option,FireDAC.Stan.Error,FireDAC.UI.Intf, 
  FireDAC.Phys.Intf, FireDAC.Stan.Def,FireDAC.Stan.Pool, FireDAC.Stan.Async, 
  FireDAC.Phys, FireDAC.Phys.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait, 
  FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, 
  FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;

type
  TForm6 = class(TForm)
    FDConnection1: TFDConnection;
    EveryQuery: TFDQuery;
  private
    { Private declarations }
  public
    { Public declarations }
    function login(username, password: String): boolean;
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

function TForm6.login(username, password: String): boolean;
begin
  Result := False;
  if (username <> '') and (password <> '') then
  begin
    //use parameter method
    EveryQuery.Close;
    EveryQuery.SQL.Text := 'Select 1 from MYGUESTS where FIRSTNAME = :theID and Password = :thePsw';
    EveryQuery.ParamByName('theID').AsString := username;
    EveryQuery.ParamByName('thePsw').AsString := password;
    EveryQuery.Open;
    Result := not EveryQuery.Eof;
    EveryQuery.Close;
    if not Result then
      ShowMessage('Wrong username or password. Please re-enter');
  end else
  begin
    ShowMessage('Please enter something');
  end;
end;

end.

那么你可以这样称呼它:

procedure TForm1.LogInClick(Sender: TObject);
begin
  if Form6.login(Username.Text, Password.Text) then
  begin
    Username.Clear;
    Password.Clear;
    Form3.Show;
    Hide;
  end;
end;