This tutorial describes how to connect to MySQL Embedded.
Contents
In order to connect to MySQL Embedded Server, you need MySQL Embedded Server files, MyDAC installed, and IDE running. Also, if authentication is enabled in the settings of MySQL Embedded Server, you need to know the login and password (by default, authentication is disabled).
It is possible to connect to MySQL Embedded Server using both TMyEmbConnection and TMyConnection components. But, in contrast to TMyConnection, TMyEmbConnection doesn't require my.ini and allows to set connection with MySQL Embedded Server via the TMyEmbConnection.Params, TMyEmbConnection.BaseDir and TMyEmbConnection.DataDir properties.
[embedded]
basedir=./
datadir=./data/
The following assumes that you have IDE running, and you are currently focused on the form designer.
TMyEmbConnection:
TMyConnection:
After you have done these steps, you should set up the newly created MyEmbConnection1 or MyConnection1 component. You can do this in two ways:
TMyEmbConnection:
TMyEmbConnection:
The same operations performed in runtime look as follows:
TMyEmbConnection:
[Delphi]
procedure TMainForm.ButtonConnectClick(Sender: TObject);
var
con: TMyEmbConnection;
begin
con := TMyEmbConnection.Create(nil);
try
con.Database := 'test'; // if the specified database does not exist, you will get an error on connecting
con.Password := 'password'; // if authentication is enabled
con.Username := 'username'; // if authentication is enabled
con.LoginPrompt := False; // to prevent showing of the connection dialog
con.Open;
finally
con.Free;
end;
end;
Note: To run this code, you have to add the MSCompactConnection and OLEDBAccess units to the USES clause of your unit.
[C++Builder]
void __fastcall TMainForm::ButtonConnectClick(TObject *Sender)
{
TMyEmbConnection* con = new TMyEmbConnection(NULL);
try
{
con->Database = "test"; // if the specified database does not exist, you will get an error on connecting
con->Password = "password"; // if authentication is enabled
con->Username = "username"; // if authentication is enabled
con->LoginPrompt = False; // to prevent showing of the connection dialog
con->Open();
}
__finally
{
con->Free();
}
}
Note: To run this code, you have to include the MyEmbConnection.hpp header file to your unit.
TMyConnection:
[Delphi]
procedure TMainForm.ButtonConnectClick(Sender: TObject);
var
con: TMyConnection;
begin
con := TMyConnection.Create(nil);
try
con.Options.Embedded := True; // enable embedded mode
con.Database := 'test'; // if the specified database does not exist, you will get an error on connecting
con.Password := 'password'; // if authentication is enabled
con.Username := 'username'; // if authentication is enabled
con.LoginPrompt := False; // to prevent showing of the connection dialog
con.Open;
finally
con.Free;
end;
end;
Note: To run this code, you have to add the MSAccess unit to the USES clause of your unit.
[C++ Builder]
void __fastcall TMainForm::ButtonConnectClick(TObject *Sender)
{
TMyConnection* con = new TMyConnection(NULL);
try
{
con->Options->Embedded = True; // enable embedded mode
con->Database = "database"; // if the specified database does not exist, you will get an error on connecting
con->Password = "password"; // if authentication is enabled
con->Username = "username"; // if authentication is enabled
con->LoginPrompt = False; // to prevent showing of the connection dialog
con->Open();
}
__finally
{
con->Free();
}
}
Note: To run this code, you have to include the MSAccess.hpp header file to your unit.
As you can see above, opening connection at run-time is as simple as calling of the Open method:
[Delphi]
con.Open;
[C++ Builder]
con->Open();
Another way to open connection at run-time is to set the Connected property to True:
[Delphi]
con.Connected := True;
[C++ Builder]
con->Connected = True;
This way can be used at design-time as well. Of course, the connection (TMyEmbConnection or TMyConnection) must have valid connection options assigned earlier. When you call Open, MyDAC tries to open the database. If any problem occurs, it raises an exception with brief explanation on what is wrong. If no problem is encountered and the database is opened, the Open method returns and the Connected property is changed to True.
To close connection, call its Close method, or set its Connected property to False:
[Delphi]
con.Close;
[C++ Builder]
con.Close();
or:
[Delphi]
con.Connected := False;
[C++ Builder]
con.Connected = False;
You can modify connection by changing the properties of the TMyEmbConnection or TMyConnection component. Keep in mind that while some of the properties can be altered freely, most of them close connection when a new value is assigned. For example, if you change the Username property, it is closed immediately, and you have to reopen it manually.
To connect to MySQL Embedded Server versions 5.6.x, it is necessary to set the MySQLEmbDisableEventLog global variable (declared in the MySqlApi module) to True before opening the connection. You can read additional information about connecting to MySQL Embedded Server in the Embedded Server article.
MyDAC has a wide set of features you can take advantage of. The following list enumerates some of them, so you can explore the advanced techniques to achieve better performance, balance network load or enable additional capabilities: