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:
- 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?
- Are 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 using C++ Builder components and Visual Bindings?
By learning more about C++ Data Bindings in windows apps, and how to compile c++ in windows. It will help you to easily build C++ applications.
How do I create a new MySQL database?
- Lets create a database to bind data
In this example I will use MySQL Database which is mostly free and popular in hosts (while Interbase, PostgreSQL, MSSQL are better nowadays). 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 a Windows Application with a MySQL connection?
- Let’s create a new C++ Builder VCL application,
2. We can use FireDAC Components to connect different databases including MySQL.
In our example, let’s use MyDAC Components (MySQL Data Access Components) to connect our MySQL database. Devart company has (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 TMyConnection, TMyDataSource, 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.
How do we create a new data binding?
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 VCL example about how you can use a database, how to connect to a database in C++ Builder and how to use data bindings. 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.