Click here to Skip to main content
14,303,684 members
   

Delphi

 
Questioncomputer name, motherboard sn, harddisk sn Pin
mazizi28-Nov-09 18:44
membermazizi28-Nov-09 18:44 
AnswerRe: computer name, motherboard sn, harddisk sn Pin
nortee14-Dec-10 4:09
membernortee14-Dec-10 4:09 
QuestionDraw a sphere in Delphi with geo-coordinate grid Pin
AgócsLaci27-Nov-09 2:52
memberAgócsLaci27-Nov-09 2:52 
AnswerRe: Draw a sphere in Delphi with geo-coordinate grid Pin
AgócsLaci27-Nov-09 2:59
memberAgócsLaci27-Nov-09 2:59 
QuestionPerformance of visual components Pin
Corinna John23-Nov-09 3:58
memberCorinna John23-Nov-09 3:58 
AnswerRe: Performance of visual components Pin
Blue_Boy2-Dec-09 9:44
memberBlue_Boy2-Dec-09 9:44 
GeneralRe: Performance of visual components Pin
Corinna John3-Dec-09 22:46
memberCorinna John3-Dec-09 22:46 
AnswerRe: Performance of visual components [modified] Pin
nortee13-Dec-10 23:24
membernortee13-Dec-10 23:24 
Hi Corina,


Corinna John wrote:
when I place a TQuery component, on a form, it consumes memory until the application quits.

The amount of memory that your query will consume is directly related to the amount of data which you are retrieving from the database, and as such cannot be helped. What you could do is look at how many fields are being returned and reducing that amount to only those fields that you need.

Corinna John wrote:
When I create/free the TQuery everytime I actually need it, the construction/destruction consumes time.

I've found that creating/destroying the components dynamically isn't time consuming. What will take time is establishing the connection to the database and the subsequent retrieval of the data from the query.

Since I hardly every use data-aware controls, most of the time I dynamically load the data to the screen(s) by creating a data controller which will provide the functionality for retrieving/updating the data from the database. Ideally this controller will have some database connection object within it, and a query/command object to query the database with. These data objects should be created and destroyed in the controller's constructor/destructor respectively. Then, by using events, you can perform some kind of action on the resulting query which you executed.

So a controller would look something like this (I see you are using the TQuery component, and although I am using ADO, the theory is still basically the same):
unit uDataController;

interface

uses
  ADODB;

type
  // my uber-super user defined event :P
  TOnRetrieveData = procedure (ASender : TObject; AResultData : TADOQuery) of object;

  TDataController = class(object)
  private
    FOnRetrieveData : TOnRetrieveData;
    FDBConnection : TADOConnection;
    FADOCommand : TADOCommand;
 
    procedure InitialiseDBStuff;
  public
    constructor Create(AOnRetrieveDataEvent : TOnRetrieveData);
    procedure ExecuteSQL(const ASQLString : string);
  end;

implementation

constructor TDataController.Create(AOnRetrieveDataEvent : TOnRetrieveData);
begin
  inherited Create;
  
  InitialiseDBStuff;
  
  // Assign the event...
  FOnRetrieveData := AOnRetrieveDataEvent;
  
end;

procedure TDataController.InitialiseDBStuff;
begin
  // Initialise the connection and command components
  FDBConnection := TADOConnection.Create(nil);
  FADOCommand := TADOCommand(nil);
  FADOCommand.CommandType := cmdText;
  FADOCommand.Connection := FDBConnection;

  // setup the connection  
  FDBConnection.ConnectionString := 'this should now be your connection string';
  // ensure that you are not prompted for the server login prompt...
  FDBConnection.LoginPrompt := False;
  // now we can open the connection...
  FDBConnection.Open;  
end;

procedure TDataController.ExecuteSQL(const ASQLString : string);
var
  LQuery : TADOQuery;
begin
  LQuery := TADOQuery.Create(nil);
  try
    FADOCommand.CommandText := ASQLString;
    LQuery.RecordSet := FADOCommand.Execute;
    if Assigned(FOnRetrieveData) then
    begin  
      while (LQuery.RecordSet <> nil) do
      begin    
        FOnRetrieveData(Self, LQuery);
        // This is just a bonus because you could get more than one result data set from the query...
        LQuery.RecordSet := LQuery..NextRecordset; 
      end;      
    end;
  finally
    FreeAndNil(LQuery);
  end;
end;


So to use the controller you would do something like this:
  uses
    // other uses stuff
    , uDataController;
  
  TMyForm = class(TForm)
    Button1 : TButton;
    Memo1 : TMemo;  
    Memo2 : TMemo;  
  private
    FController : TDataController;    
    procedure Perform_OnRetrieveData(ASender : TObject; AResultData : TADOQuery);
    procedure MyButtonClick(ASender : TObject);
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;
  
procedure TMyForm.Perform_OnRetrieveData(ASender : TObject; AResultData : TADOQuery);
var
  LRows : Integer;
  LIdx : Integer;
  LLine : string;
begin
  LLine := '';
  Memo2.Lines.Add('Start a data result output...');
  Memo2.Lines.Add('-----------------------------');
  AResultData.First;
  while not(AResultData.EoF) do
  begin 
    for LIdx := 0 to AResultData.FieldCount-1 do
    begin
      LLine := LLine+'  '+AResultData.Fields[LIdx].Value;
    end;
    Memo2.Lines.Add(LLine);
    AResultData.Next;
  end;
  Memo2.Lines.Add(' End a data result output...');
  Memo2.Lines.Add('-----------------------------');
  Memo2.Lines.Add('');
end;

procedure TMyForm.MyButtonClick(ASender : TObject);
begin
  Memo2.Lines.Clear;
  try
    FController.ExecuteSQL(Memo1.Text);
  except
    on E:Exception do
    begin
      Memo2.Lines.Add('Exception occurred when trying to execute sql script. Reason:');
      Memo2.Lines.Add(E.Message); 
    end;
  end;
end;

procedure TMyForm.AfterConstruction; 
begin
  FController := TDataController.Create(Perform_OnRetrieveData);
  Button1.OnClick := MyButtonClick;  
end;

procedure TMyForm.BeforeDestruction; 
begin
  FreeAndNil(FController);
end;


DISCLAIMER: I have not tested this code, so if it contributes to global warming for some reason, I relinquish myself from any consequences it may have on the tragic collapse of the lifecycle of the common sea cucumber... Laugh | :laugh:

Hope that helps Smile | :)
Cheers,
Glen Vlotman
"You cannot code for stupidity"
modified on Tuesday, December 14, 2010 7:55 AM

QuestionColor in TabbedNotebook Pin
fckgwzz2-Nov-09 18:51
memberfckgwzz2-Nov-09 18:51 
QuestionTAggregateField Pin
Kylie Ryan23-Sep-09 14:54
memberKylie Ryan23-Sep-09 14:54 
AnswerRe: TAggregateField Pin
Kylie Ryan27-Sep-09 15:45
memberKylie Ryan27-Sep-09 15:45 
QuestionCan Delphi do what C++ can't? Pin
Member 464187815-Sep-09 9:45
memberMember 464187815-Sep-09 9:45 
AnswerRe: Can Delphi do what C++ can't? Pin
egenis15-Sep-09 19:28
memberegenis15-Sep-09 19:28 
GeneralRe: Can Delphi do what C++ can't? Pin
Member 464187816-Sep-09 4:07
memberMember 464187816-Sep-09 4:07 
AnswerRe: Can Delphi do what C++ can't? Pin
Eddy Vluggen16-Sep-09 5:02
mveEddy Vluggen16-Sep-09 5:02 
AnswerRe: Can Delphi do what C++ can't? Pin
Rozis16-Sep-09 13:59
memberRozis16-Sep-09 13:59 
AnswerRe: Can Delphi do what C++ can't? Pin
Member 464187818-Sep-09 5:13
memberMember 464187818-Sep-09 5:13 
Questiondebug a dll in Delphi Pin
MrKBA7-Sep-09 2:56
memberMrKBA7-Sep-09 2:56 
AnswerRe: debug a dll in Delphi Pin
mobius1110018-Sep-09 2:52
membermobius1110018-Sep-09 2:52 
Questionmixed string format in richview control Pin
askhar51630-Aug-09 16:35
memberaskhar51630-Aug-09 16:35 
QuestionTDBEdit Copy and Paste Pin
dave.kelly13-Aug-09 4:50
professionaldave.kelly13-Aug-09 4:50 
AnswerRe: TDBEdit Copy and Paste Pin
dave.kelly17-Aug-09 3:07
professionaldave.kelly17-Aug-09 3:07 
QuestionDelete stuff in memobox Pin
aerosmith2k14-Aug-09 17:53
memberaerosmith2k14-Aug-09 17:53 
AnswerRe: Delete stuff in memobox Pin
Blue_Boy5-Aug-09 0:18
memberBlue_Boy5-Aug-09 0:18 
Questiondelphi book Pin
herupokiojio3-Aug-09 11:53
memberherupokiojio3-Aug-09 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.