----- Original Message ----- 
From: "a.j. welmers" <[EMAIL PROTECTED]>

> --- In [email protected], "David Bolton" <[EMAIL PROTECTED]> wrote:
> "Reliability and robustness. Its all OO either way but if you write a
> lot of code at the form level its more of a hack than if you write
> bespoke classes. Its less important if you have one query, but if
> you're working with multiple tables, are you going to reuse the same
> query component ...............
> David Bolton"
>
> David can you make me  some code snippets or a small application
> available, that I can study to understand better what you mentioned. I
> am still in the Delphi 5 + using out of the box components [dbgris +
> dbnavigator ] and like to go in the direction you mentioned.
> thank you
> jan welmers

This is an example using DAC for Mysql.
It has hardcoded values (which is naughty) against a database testdb, though 
easy enough to making them properties.

I'd typically use it as a member of a class.

David

Use it like this.

In the class

fladb : tladb;

,,,
  fladb.select('your sql query');
  if fladb.count >0 then
   begin
     fladb.query.first;
     while not fladb.query.eof do
        begin
          ...
           fladb.query.next;
        end;
     fladb.




unit dacdb;

interface

uses
  mySQLDbTables,Db, classes;

Type TLaDb = class
   private
     FDataBase  : TMySqldatabase;
     FDB        : TMysqlQuery;
     FLastError : string;
    function GetCount: integer;

   public
     Constructor Create;
     Destructor  Destroy;override;
     function    Select(SQL : string ) : integer;
     function    Exec(sql : string) : boolean;
   property Query : TMySqlQuery read FDB;
   property LastError : string read FLastError write FLastError;
   property Count : integer read GetCount;
end;

implementation

uses  Sysutils;

{ TLADb }

constructor TLADb.Create;
begin
{    Connected = True
    DatabaseName = 'database'
    UserName = 'root'
    UserPassword = 'xys'
    Host = 'localhost'
    ConnectOptions = []
    Params.Strings = (
      'Port=3306'
      'TIMEOUT=30'
      'DatabaseName=testdb'
      'UID=root'
      'PWD=xys'
      'Host=localhost')
 }
  FDatabase          := TMySqlDatabase.Create(nil);
  FDatabase.Host     := 'localhost';
  FDatabase.UserName     := 'root';
  FDatabase.UserPassword := 'xys';
  FDatabase.DatabaseName := 'testdb';
  FDatabase.Port     := 3306;
  FDatabase.Connected := True;

  Fdb                := TMySqlQuery.Create(nil);
  Fdb.Database     := FDatabase;
end;

destructor TLADb.Destroy;
begin
  FDb.Free;
  FDatabase.Free;
end;

function TLADb.Exec(sql: string): boolean;
begin
  Fdb.Active := false;
  Fdb.Sql.Text := SQL;
  try
    Fdb.ExecSql;
    FLastError := '';
    result     := true;
  except
    on E : Exception do
      begin
        Result := False;
        FLastError := E.Message;
      end;
  end;
end;

function TLADb.Select(SQL: string): integer;
begin
  Fdb.Active := false;
  Fdb.Sql.Text := SQL;
  try
    Fdb.Open;
    FLastError := '';
    result     := Fdb.RecordCount;
  except
    on E : Exception do
      begin
        Result := 0;
        FLastError := E.Message;
      end;
  end;
end;

function TLaDb.GetCount: integer;
begin
  result := fdb.RecordCount;
end;

end.




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page
http://us.click.yahoo.com/dpRU5A/wUILAA/yQLSAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to