Learn To Use MySQL Database Connections On Windows With MyDAC Components
February 28, 2021 By Yilmaz Yoru
In this post we would like to give a very simple example to connect to a MySQL database in C++ Builder with MyDAC Component.
Для просмотра ссылки Войдиили Зарегистрируйся is one of the world’s most popular open source database and it is very easy and very useful to use small to large scale databases. MySQL has MySQL Service and different editions ( Enterprise, Standard, Classic, Cluster CGE and Embedded versions). MySQL Classic Edition is good to begin with database applications, the ideal embedded database for ISVs, OEMs and VARs developing read-intensive applications using the MyISAM storage engine. The Для просмотра ссылки Войди или Зарегистрируйся has lower TCO, ease of use, requires lower administration skills, and it supports over 20 platforms and operating systems. It is known as a high performance and zero administration database.
More details can be found on their official web page (Для просмотра ссылки Войдиили Зарегистрируйся) , it can be downloaded from Для просмотра ссылки Войди или Зарегистрируйся free and it can be installed on Windows and other OSes for learning and for some other benefits.
In Для просмотра ссылки Войдиили Зарегистрируйся, you can connect to MySQL database by using official FireDAC Component with it’s proper dynamic MySQL library. This will be explained in another post. There might be other 3rd party components too.
Для просмотра ссылки Войдиили Зарегистрируйся company has MySQL Data Access Components (MyDAC) for RAD Studio, C++ Builder and it is a library of components that provides direct access to MySQL and MariaDB including Community Edition, as well as Lazarus (and Free Pascal) on Windows, Linux, macOS, iOS, and Android for both 32-bit and 64-bit platforms. C++ Builder applications based on MyDAC can connect directly to MySQL server or work through the MySQL client library. MyDAC is designed to help programmers develop faster and cleaner MySQL database applications. MyDAC is a complete replacement for standard MySQL connectivity solutions and presents an efficient alternative to the Borland Database Engine (BDE) and standard dbExpress driver for access to MySQL. Full features and other documentations of MyDAC Component can be found their official web page (Для просмотра ссылки Войди или Зарегистрируйся) and it has a trail version running with limits.
To use MyDAC with C++ Builder, first you must download trial or purchased version from their site and you must install it for both VCL and FMX Projects. In a new C++ Builder Project (VCL or FMX), you can drag and use it’s components on your forms, or professionally you can add a new DataModule to your project and you can drag and use them by adding it’s header to other required forms.
If you created a MySQL database with a table and you have installed MyDAC Components in your Component Palette, Let’s see steps to connect to a MySQL database and query a table.
1. Create a new C++ Builder Project in VCL or FMX.
2. Basically to connect to a database table in a MYSQL server, we need 4 MyDAC Components . Drag MyConnection (TMyConnection), MyDataSource (TMyDataSource) , MyQuery (TMyQuery), MyCommand (TMyCommand) components on to your Form (TForm).
3. Now Set Connection property of MyQuery1 MyConnection1 from Object Inspector
and Set DataSet property of MyDataSource1 as MyQuery1 from Object Inspector
4. Double click to MyConnection component to enter MySQL server connection details. Enter parameters as below;
Server:
If you connect to a remote MySQL Server type its mysql IP or mysql address
If you connect to a server on your computer- type localhost or your computer IP address
Port:
It is generally 3306, if you or your host setup a different port number check your port number from mySQL administration or host settings.
Username and Password:
If you haven’t create any username for your specific table, including with user privileges, you can use root and it’s password. If you have user name for this table, you can use this user name and it’s password to connect.
That’s all, now you can check your connection directly by enabling or disabling it’s Connected feature.
5. Now you can directly connect in C++ Builder with this component by using MySQLConnection1 component property as below;
You can check if it is connected by if clauses as below
For example you can do this on a button as below;
6. MySQL using its own SQL language to query tables in it’s database, for example if you have a table with a name mytable, you can list all data by using this query
This query can be directly added by double clicking to SQL property of MyQuery1,
or, you can add your own query in your code lines by double clicking to a Button, as below;
Full video about this application can be found from CodeRage 2018 “Connecting to MySQL Database Using MyDAC Components” presentation.
February 28, 2021 By Yilmaz Yoru
In this post we would like to give a very simple example to connect to a MySQL database in C++ Builder with MyDAC Component.
Для просмотра ссылки Войди
More details can be found on their official web page (Для просмотра ссылки Войди
In Для просмотра ссылки Войди
Для просмотра ссылки Войди
To use MyDAC with C++ Builder, first you must download trial or purchased version from their site and you must install it for both VCL and FMX Projects. In a new C++ Builder Project (VCL or FMX), you can drag and use it’s components on your forms, or professionally you can add a new DataModule to your project and you can drag and use them by adding it’s header to other required forms.
If you created a MySQL database with a table and you have installed MyDAC Components in your Component Palette, Let’s see steps to connect to a MySQL database and query a table.
1. Create a new C++ Builder Project in VCL or FMX.
2. Basically to connect to a database table in a MYSQL server, we need 4 MyDAC Components . Drag MyConnection (TMyConnection), MyDataSource (TMyDataSource) , MyQuery (TMyQuery), MyCommand (TMyCommand) components on to your Form (TForm).
3. Now Set Connection property of MyQuery1 MyConnection1 from Object Inspector
and Set DataSet property of MyDataSource1 as MyQuery1 from Object Inspector
4. Double click to MyConnection component to enter MySQL server connection details. Enter parameters as below;
Server:
If you connect to a remote MySQL Server type its mysql IP or mysql address
If you connect to a server on your computer- type localhost or your computer IP address
Port:
It is generally 3306, if you or your host setup a different port number check your port number from mySQL administration or host settings.
Username and Password:
If you haven’t create any username for your specific table, including with user privileges, you can use root and it’s password. If you have user name for this table, you can use this user name and it’s password to connect.
That’s all, now you can check your connection directly by enabling or disabling it’s Connected feature.
5. Now you can directly connect in C++ Builder with this component by using MySQLConnection1 component property as below;
C++:
MySQLConnection1->Connected=true;
C++:
if(MySQLConnection1->Connected)
{
// Open a Table or tables and do some other commands
}
else
{
// Show message that you can't connect, may be use has wrong username or password
}
C++:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(DM->MyConnection1->Connected)
{
DM->MyConnection1->Disconnect();
Button1->Text=u"Connect";
}
else
{
DM->MyConnection1->Connect();
Button1->Text=u"Disconnect";
}
}
SQL:
select * from mytable;
or, you can add your own query in your code lines by double clicking to a Button, as below;
C++:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DM->Query1->SQL->Clear();
DM->Query1->SQL->Add("Select * from mytable;");
DM->Query1->Open();
}