We have posts that explain how you can connect to different databases like MySQL, Interbase, PostgreSQL, MSSQL with different components like FireDAC, MyDAC, etc. If you need more help about this topic Please check here https://learncplusplus.org/category/database/ about these database posts.
In this post, you’ll get answers to these questions:
- How can I use visual bindings with a database?
- What is LiveBindings? How can I use LiveBindings Designer?
- Can I use FireDAC components with Visual Bindings?
- Is there visual bindings in C++ Builder like there are in Objective-C++?
- Can I use MyDAC components with Visual Bindings?
- Can I use all components with Visual Bindings in C++ Builder?
- How can I create a database and connect to it by using C++ Builder components and Visual Bindings?
- Can I use DataBindings in FireMonkey Applications?
- How can I connect to database with my Multi-Device application?
By learning how to easily connect to a MySQL Database in c++, it will help you to build C++ applications with the use of a C++ IDE.
Table of Contents
What are data bindings?
We can connect to local or online databases by using Data Bindings. Data bindings allow us to connect the properties of our components and code to database fields and records in an automatic, low code way simply by describing how those interconnections work – “this database field is linked to the text property of this visual edit control”. Data bindings are an extremely powerful way of manipulating data in a way which means we write little or even NO code.
We can easily connect to a database by database components of C++ Builder. C++ Builder components supports many different database connections from old ones to new and the latest versions. FireMonkey is another framework as VCL framework. Both frameworks have a lot of data tools, libs, components and visuals to display and manipulate database. These high standards in database operations makes your C++ applications strong and flexible with visuals of components. Visual styles are another level of low code technique which can make your applications visually stunning for different platforms like Windows, Android, iOS, MacOS in different devices like Desktops, Laptops, Tablets, Cellphones, Android and iOS Powered Watches, and other compatible ARM based devices.
All these are features of C++ Builder and they allow developers to produce a single code base application which can be built to run on many target operating systems and devices. I think all these can explain how FireMonkey UI Framework is powerful in C++ development. There is currently no perfect solution which can create cross platform apps under every set of circumstances but FireMonkey goes a long way down the road to getting us there.
Data Bindings can be done by setting properties of components on design time of the applications or on the run time of the applications. We can also create data bindings by using visual bindings in C++ Builder that is called LiveBindings Designer. Thus allows users to see data connections of each data members directly. Users can also detect connection problems, query problems and other database related errors without doing compilation. This feature speeds up your development process on these database connected applications. The best one is using database with the fastest C++ programming language results with faster data connected applications.
How do I create a MySQL Database?
- Lets create a database to bind data
In this example I will use a MySQL Database which is free for most use-cases and popular among web hosting companies. Lets create a “users” table in the “test” schema.
Here we add some columns like USERID, USERNAME, AGE. You can add your own columns.
2. Let’s add some data to our simple database,
How do I create an C++ FMX multi-platform application which connects to a MySQL database?
- Let’s create a new C++ Builder Mutli-Device FireMonkey application,
2. We can use FireDAC Components to connect different databases including MySQL. We can use other 3rd party components too.
In our example let’s use MyDAC Components to connect our MySQL database. 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.
Let’s drag a TMyConnection, TMyDataSource and TMyQuery components to your form, Let’s add a TStringGrid to see database columns. We can use a lot of components to reach any database elements.
Now our application will be as below,
3. OurDataSource wil be optained from SQL in the MyQuery1. So select MyDataSource1 and set its DataSet to MyQuery1
4. Now we should connect our MyQuery1 to MySQL database. Select MyQuery1 and set Connection property to MyConnection1.
5. Double click to MyQuery1 to enter a SQL query. Let’s add a simple SQL query, Our schema is “test” and table is “users“, then let’s write “select * from test.users;” and press OK.
6. Now we should enter our MySQL connection settings. To do this, this time double click to MyConnection1. You will see this connection window,
Here, enter your Server, Username, Password. If all is correct you should able to select Database. Select your database and press Connect. You will see green dot instead of red dot here. Then press OK to setup this connection.
7. Now our MyConnection1 should be Active, we can connect to a table, thus we can activate our MyQuery1 by setting it’s Active property to true; If the MyQuery1 is active then we established connection to our database on design time. If you run this application, your application will maintain all settings and it will start with this connected state.
What is data binding and why is it a low code way to write programs which store and retrieve data?
8. Finally we can use visual LiveBindings Designer to connect our Database. Right click to StringGrid1 or MyQuery1 component, and select Bind Visually… to activate LiveBindings Designer Form as below,
9. Now there will be LiveBindings Designer and the components that can be connected with each other will be display in here. Left click to * of MyQuery1 and do not release left mouse button,
Now drag your mouse to * of StringGrid1 that will allow to connect StringGrid1 with MyQuery1. Now you can release the Left Mouse Button.
10. You will see that LiveBindings Designer will create BindSourceDB1 automatically and it will connect to this to StringGrid1. At the same time on the Designer Form you will see that StringGrid columns will be changed and the members of table will be listed directly.
This is a very simple Firemonkey example about how you can use a database, how to connect to a database in C++ Builder and how to use data bindings. This example can be compiled for different platforms in C++ Builder, as a Windows 32-bits, Windows 64-bits, Andorid, iOS or as a MacOS application.
Note that you can bind each members of this BindSourceDB1 members to any components. For example USERNAME to Label2->Caption, AGE to Label3->Caption. For more detailed examples please check Database Section of LearnCPlusPlus.org and DocWiki of Embarcadero. There are many official and 3rd party components that supports many ways to display your data.
C++ Builder has a lot of components and ways to connect different databases and to visualize results with many components like TTeeChart, TViewport3D, … and as a 3rd party components TMS components etc. RAD Studio 11 have new updates on the most of these kind of components while it is just released. Embarcadero and 3rd party component developers are updating these tools. They can be updated by using GetIt Package Manager easily.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition