C++ComponentsDatabaseLanguage FeatureLearn C++

Learn To Use MySQL Database Connections In C++ On Windows

In this post we would like to give a very simple example to connect to a MySQL Database in C++ Builder with FireDAC Component which comes with RAD Studio officially.

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.

FireDAC component pack is one of the great component for database connections that comes with RAD Studio, C++ Builder or Delphi officially. FireDAC is a Universal Data Access library for developing applications for multiple devices, connected to enterprise databases. With its powerful universal architecture, FireDAC enables native high-speed direct access from Delphi and C++Builder to InterBase, SQLite, MySQL, SQL Server, Oracle, PostgreSQL, DB2, SQL Anywhere, Advantage DB, Firebird, Access, Informix, DataSnap and more, including the NoSQL Database MongoDB.

FireDAC is a Universal Enterprise Data Connectivity

This image has an empty alt attribute; its file name is FireDAC_Architecture12-21-16.png

To use FireDAC with C++ Builder, be sure that your RAD Studio, C++ Builder version has support to this component, we highly recommend here C++ Builder 10.x or above because of improvements on database connections. If you dont have this component in you version there is a Trial version of FireDAC officially that you can test and then you can purchase. 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 FireDAC Components in your Component Palette, Let’s see steps to connect to a MySQL database and query a table.

1. To use MySQL with FireDAC you need it’s libmysql.dll library. This library can be found in the lib folder of MySQLConnector C tool. Copy this library to your application folder or given Windows folders below;

2. Create a new C++ Builder Project in VCL or FMX.

3. Basically to connect to a database table in a MySQL server, we need 4 FireDAC Components . Drag FDConnection (TFDConnection), DataSource (TDataSource) , FDQuery (TFDQuery), FDCommand (TFDCommand) components on to your Form (TForm) or to your DataModule (TDataModule).

4. Set Connection property of FDQuery1 as FDConnection1
and Set DataSet property of DataSource1 to FDQuery1

5. Double click to FDConnection component to enter MySQL server connection details.

Enter parameters as below;

Driver ID:
We will connect to MySQL database, so Driver ID should be MySQL
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- set <LOCAL> 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 username for this table, you can use this username and it’s password to connect.

That’s all, now you can check your connection directly by enabling or disabling it’s Connected feature.

6. Now you can directly connect in C++ Builder with this component by using FDConnection1 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;

7. 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 to SQL property of FDQuery1 by double clicking to FDQuery1,

or, you can add your own SQL query in your code lines by double clicking to a Button, as below;

Full video about FireDAC can be found here Introduction to FireDAC – Multi-device data access for Delphi and C++Builder

First Steps to Use FireDAC
http://docwiki.embarcadero.com/RADStudio/Sydney/en/First_Steps_to_Use_FireDAC

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?