Topic: MySQL database connection button.

Hello, friends! Help with the question. For example: I work with the SQLite database, but there is also a MySQL database and I would like to connect to it through the button with a script in the program. But how can I get data from the MySQL database to which I connected with the button, if there is Form1.TableGrid1? Is it possible? Thank.


I connect to the database like this:

procedure Form1_Button9_OnClick (Sender: TObject; var Cancel: boolean);
var
    MySQL: TMyConnection;
begin
    MySQL := TMyConnection.Create(Form1);
    try
            MySQL.Options.UseUnicode := True;
            MySQL.Server := '127.0.0.1'; 
            MySQL.Port := 3306;
            MySQL.Username := 'user';
            MySQL.Password := 'pass';
            MySQL.Database := 'base';
            MySQL.LoginPromt := False;

            try
                MySQL.Connect;
            except
                ShowMessage('Can''t connect to database.');
            end;

            if MySQL.Connected then
            begin
               ShowMessage ('DB OK');
            end;

    finally
        //MySQL.Free;
    end;
end;

With Google translator.

Re: MySQL database connection button.

vohans,
Insert the following after you are connected.

Form1.TableGrid1.dbUpdate;

Re: MySQL database connection button.

ehwagner wrote:

vohans,
Insert the following after you are connected.

Form1.TableGrid1.dbUpdate;

Thank you, but I need to load the data into TableGrid already from MySQL to which we are connected. "dbUpdate" updates TableGrid from SQLite database.

Re: MySQL database connection button.

vohans,
dbUpdate also updates the tablegrid from MySQL. If you have the tablegrid settings defined on the form, then placing the dbUpdate after your connection, as follows, will display the records in the tablegrid.

 if MySQL.Connected then
            begin
               Form1.TableGrid1.dbUpdate;  // This will display the MySQL records if you have the fields defined in the settings of the tablegrid.
               //ShowMessage ('DB OK');
            end;

Re: MySQL database connection button.

ehwagner,  Thank you.