In this post we would like to give a very simple example to connect to a MySQL database in C++ Builder with MyDAC Component.
MySQL 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 MySQL Classic Edition 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 (https://www.mysql.com/) , it can be downloaded from https://dev.mysql.com/downloads/installer/ free and it can be installed on Windows and other OSes for learning and for some other benefits.
In C++ Builder, 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.
Devart 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 (https://www.devart.com/mydac/) 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;
1 |
MySQLConnection1->Connected=true; |
You can check if it is connected by if clauses as below
1 2 3 4 5 6 7 8 |
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 } |
For example you can do this on a button as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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"; } } |
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
1 |
select * from mytable; |
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;
1 2 3 4 5 6 |
void __fastcall TForm1::Button2Click(TObject *Sender) { DM->Query1->SQL->Clear(); DM->Query1->SQL->Add("Select * from mytable;"); DM->Query1->Open(); } |
Full video about this application can be found from CodeRage 2018 “Connecting to MySQL Database Using MyDAC Components” presentation.
Head over and find out more about building modern c++ applications for Windows.