LiteQuery MAX(Integer)、MAX(String) 判断是否返回值

unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, LiteCall, LiteConsts, Vcl.StdCtrls,
  Data.DB, MemDS, DBAccess, LiteAccess;

type
  TForm6 = class(TForm)
    LiteConnection1: TLiteConnection;
    LiteQuery1: TLiteQuery;
    Button1: TButton;
    LiteQuery2: TLiteQuery;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

procedure TForm6.Button1Click(Sender: TObject);
begin
  LiteQuery1.Close;
  LiteQuery1.SQL.Text := 'SELECT MAX(age) AS max_age FROM max_test';
  LiteQuery1.Open;
  if LiteQuery1.IsEmpty then
  begin
    ShowMessage('IsEmpty');
  end else begin
    ShowMessage('非空');
    ShowMessage(LiteQuery1.FieldByName('max_age').AsString);
    if LiteQuery1.FieldByName('max_age').IsNull then
    begin
      ShowMessage('为null');
    end else begin
      ShowMessage(LiteQuery1.FieldByName('max_age').AsString);
    end;
  end;
end;

procedure TForm6.Button2Click(Sender: TObject);
begin
  LiteQuery2.Close;
  LiteQuery2.SQL.Text := 'SELECT MAX(name) AS max_name FROM max_test';
  LiteQuery2.Open;
  if LiteQuery2.IsEmpty then
  begin
    ShowMessage('IsEmpty');
  end else begin
    ShowMessage('非空');
    ShowMessage(LiteQuery2.FieldByName('max_name').AsString);
    if LiteQuery2.FieldByName('max_name').IsNull then
    begin
      ShowMessage('为null');
    end else begin
      ShowMessage(LiteQuery2.FieldByName('max_name').AsString);
    end;


  end;
end;

end.
原文地址:https://www.cnblogs.com/del88/p/7397171.html