Topic: [Script] How to connect to MySQL using script

Example, how to connect to MySQL server using script.


procedure Form1_OnShow (Sender: string; Action: string);
begin
     frmWait.Show;
     Application.ProcessMessages;

     {$MySQL disable_connectdialog}
     Form1.MySQLConnection.Server := '127.0.0.1';
     Form1.MySQLConnection.Port := 3306;
     Form1.MySQLConnection.Username := 'root';
     Form1.MySQLConnection.Password := 'root';
     Form1.MySQLConnection.Database := 'mvd';

     try
         Form1.MySQLConnection.Connect;
     except
         frmWait.Close;
         ShowMessage('Can''t connect to database.');
         Form1.Close;
     end;

     if Form1.MySQLConnection.Connected then
     begin
         UpdateDatabase(''); // to fill ComboBoxes
         Form1.GridEmployees.dbUpdate; // if you have TableGrid on first form with option "Enable auto execution", you should call method dbUpdate manually
     end;
     frmWait.Close;
end;




begin


end.


Download project:
http://myvisualdatabase.com/forum/misc. … download=1

Dmitry.

2 (edited by mathmathou 2015-12-30 00:52:20)

Re: [Script] How to connect to MySQL using script

Hello Dmitry,


Sorry to dig up that post, I have an idea with that.


I work on a local SQLite database, but from time to time, I want to export simple data to an online MYSQL database.


I tested your script, and I can connect to a remote server easily, this works fine.


But I see you put your MYSQL connection script between

begin

end.

so it is at the end of the script file and the connection is always active right ?


Is it possible to use this same script but on a button click for example and how do I close the connection when finished ?

I see

try
Form1.MySQLConnection.Connect

Is there some kind of

try
MySQLConnection.Connect
MySQLConnection.Disconnect

Thanks in advance


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: [Script] How to connect to MySQL using script

Unfortunately in the current version you can't switching between SQLite and MySQL using script.

Dmitry.

Re: [Script] How to connect to MySQL using script

Thanks Dmitry for the answer.


No problem, I'll add another MVD app, launched from the first one, that will connect to MYSQL for that specific need.


So without switching, is it possible to connect via a button, and disconnect via another one, or do I have to put the connection info on Form1 between

begin

end.

?

And what is the command to disconnect ?


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: [Script] How to connect to MySQL using script

Yes, you can use button to connect to database.


To disconnect:

Form1.MySQLConnection.Disconnect; 
Dmitry.

Re: [Script] How to connect to MySQL using script

Thanks Dmitry smile


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: [Script] How to connect to MySQL using script

Hello,

I try to use this script to connect to mysql but im receiving "exception EAccessViolation..." when UpdateDatabase(''); is used.
without this, there is no error. but combobox cant show data.
Any other way to repopulate data?

Thanks in advance!

Re: [Script] How to connect to MySQL using script

rhyacm wrote:

Hello,

I try to use this script to connect to mysql but im receiving "exception EAccessViolation..." when UpdateDatabase(''); is used.
without this, there is no error. but combobox cant show data.
Any other way to repopulate data?

Thanks in advance!

Please attach your project, I will test it. Thanks.

Dmitry.

Re: [Script] How to connect to MySQL using script

DriveSoft wrote:
rhyacm wrote:

Hello,

I try to use this script to connect to mysql but im receiving "exception EAccessViolation..." when UpdateDatabase(''); is used.
without this, there is no error. but combobox cant show data.
Any other way to repopulate data?

Thanks in advance!

Please attach your project, I will test it. Thanks.

it is working fine when i put the connection on form load.  i got the error when i put the connection script at this part...

begin

//sql connection
//sql query

end.

because i want to insert data first if table user is empty.
so i put the connection script there and it return the error.
so i guess im not allowed to put the connection script there.

Re: [Script] How to connect to MySQL using script

Yes, you can place this script between begin end.

Dmitry.