C++ComponentsDatabaseLanguage FeatureLearn C++

Learn To Use MySQL Database Connections On Windows With MyDAC Components

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;

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
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.

Head over and find out more about building modern c++ applications for Windows.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond