Lazarus

Free Pascal => Database => Topic started by: WimVan on December 20, 2018, 10:28:00 pm

Title: Why can't I change hte SQLConnetor to point to another database
Post by: WimVan on December 20, 2018, 10:28:00 pm
I want using an application to access several databases.  Why can't I do that ?

I use win10, Lazarus 1.8.4
TSQLconnector, dataabse Mysql 5.7 and have the correct libmysql.Dll

Here is the code
Code: Pascal  [Select][+][-]
  1.   MasterQuery.Close;
  2.   MasterQuery.Close;
  3.   MasterSQLTransaction.Active := False;
  4.   DBConnection.Connected      := False;
  5.   DBConnection.close(true);
  6.   DBConnection.DatabaseName   := 'pmoisr';
  7.   DBConnection.HostName       := 'Localhost';
  8.   DBConnection.UserName       := 'root';
  9.   DBConnection.Connected      := True;
  10.   DBConnection.Open;
  11.   MasterSQLTransaction.Active := True;
  12.   MasterQuery.Active          := True;
  13.  

There is no compile or build-error
Application pops up and closed directly.

What's wrong ?
Title: Re: Why can't I change hte SQLConnetor to point to another database
Post by: valdir.marcos on February 22, 2019, 11:02:25 pm
I want using an application to access several databases.  Why can't I do that ?

I use win10, Lazarus 1.8.4
TSQLconnector, dataabse Mysql 5.7 and have the correct libmysql.Dll

Here is the code
Code: Pascal  [Select][+][-]
  1.   MasterQuery.Close;
  2.   MasterQuery.Close;
  3.   MasterSQLTransaction.Active := False;
  4.   DBConnection.Connected      := False;
  5.   DBConnection.close(true);
  6.   DBConnection.DatabaseName   := 'pmoisr';
  7.   DBConnection.HostName       := 'Localhost';
  8.   DBConnection.UserName       := 'root';
  9.   DBConnection.Connected      := True;
  10.   DBConnection.Open;
  11.   MasterSQLTransaction.Active := True;
  12.   MasterQuery.Active          := True;
  13.  

There is no compile or build-error
Application pops up and closed directly.

What's wrong ?
I have attached a very small example which works for Firebird, try to adapt it for your MySQL case.

Code: SQL  [Select][+][-]
  1. CREATE TABLE Table1
  2. (
  3.   Field1 INTEGER NOT NULL PRIMARY KEY,
  4.   Field2 VARCHAR(50) NOT NULL
  5. );
  6. Commit;
  7.  
  8. INSERT INTO Table1(Field1, Field2) VALUES(1, 'aaa');
  9. INSERT INTO Table1(Field1, Field2) VALUES(2, 'bbb');
  10. INSERT INTO Table1(Field1, Field2) VALUES(3, 'ccc');
  11. INSERT INTO Table1(Field1, Field2) VALUES(4, 'ddd');
  12. INSERT INTO Table1(Field1, Field2) VALUES(5, 'eee');
  13. Commit;
  14.  
  15. SELECT * FROM Table1;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, IBConnection, sqldb, db, FileUtil, Forms, Controls,
  9.   Graphics, Dialogs;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     DataSource1: TDataSource;
  17.     IBConnection1: TIBConnection;
  18.     SQLQuery1: TSQLQuery;
  19.     SQLTransaction1: TSQLTransaction;
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.   IBConnection1.Connected := False;
  39.   IBConnection1.HostName := 'localhost/3050';
  40.   IBConnection1.DatabaseName := 'C:\teste\FirebirdMultiDatabaseExample\SampleData1.fdb';
  41.   IBConnection1.UserName := 'SYSDBA';
  42.   IBConnection1.Password := 'masterkey';
  43.   IBConnection1.CharSet := 'UTF8';
  44.   IBConnection1.Connected := True;
  45.  
  46.   SQLQuery1.Close;
  47.   SQLQuery1.SQL.Text := 'Select Field2 From Table1 Where Field1 = 1;';
  48.   SQLQuery1.Open;
  49.   ShowMessage(SQLQuery1.FieldByName('Field2').AsString);
  50.   SQLQuery1.Close;
  51.   SQLTransaction1.Commit;
  52.   IBConnection1.Connected := False;
  53.  
  54.   IBConnection1.Connected := False;
  55.   IBConnection1.DatabaseName := 'C:\teste\FirebirdMultiDatabaseExample\SampleData2.fdb';
  56.   IBConnection1.Connected := True;
  57.  
  58.   SQLQuery1.Open;
  59.   ShowMessage(SQLQuery1.FieldByName('Field2').AsString);
  60.   SQLQuery1.Close;
  61.   SQLTransaction1.Commit;
  62.   IBConnection1.Connected := False;
  63.  
  64.   IBConnection1.Connected := False;
  65.   IBConnection1.DatabaseName := 'C:\teste\FirebirdMultiDatabaseExample\SampleData3.fdb';
  66.   IBConnection1.Connected := True;
  67.  
  68.   SQLQuery1.Open;
  69.   ShowMessage(SQLQuery1.FieldByName('Field2').AsString);
  70.   SQLQuery1.Close;
  71.   SQLTransaction1.Commit;
  72.   IBConnection1.Connected := False;
  73. end;
  74.  
  75. end.
Title: Re: Why can't I change hte SQLConnetor to point to another database
Post by: Zvoni on February 23, 2019, 07:17:36 am
If it's TSQLConnector, don't you have to specify the connection-type?
Title: Re: Why can't I change hte SQLConnetor to point to another database
Post by: valdir.marcos on February 23, 2019, 07:26:52 am
If it's TSQLConnector, don't you have to specify the connection-type?
Yes, you have to, but you don't always need to use TSQLConnector for a multi database approach, as I just showed.
TinyPortal © 2005-2018