C++ Builder is a great developer tool that 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.
You can easily access almost every type of database with C++ Builder
In our previous posts, we have described well how to connect to Interbase, PostgreSQL databases with FireDAC component that comes with RAD Studio officially, and how to connect to MySQL with FireDAC and MyDAC components. You can easily connect and query fields and values by using SQL queries. In C++ Builder, in professional applications, we should make each query simple and detailed for the user’s benefit. So they can choose more details, they can filter their query, or they can add their own SQL commands. Generally, in most applications users are far from knowing about SQL commands; thus developer should add more functionality to his application that filters in accordance with the database files and with the user needs from that application. You can check our DATABASE section for mode details about connections and databases.
An example SQL query C++ application
In this post we will develop a simple application that can do more detailed SQL queries. To do this first you should know well about each components, some components may have database components that can be used with bindings.
In our example we will not connect to a database but we will show the query text. So you can use our previous database examples to connect a database and you can add these example features to your applications to query by using components. As seen Form example below, we have a Data Table and Sub Data Table, we have a Text Search, Line Limits, Dates Begins and Ends also a checkbox for this date checking. And we have a Button to get SQL with these options from the componenets and a Memo to see this query.
This SQL Query can be obtained from these components as below in this Button1Click(),
void __fastcall TForm1::Button1Click(TObject *Sender)
sql = "SELECT user_id, user_name, user_surname, user_adress, record_date from "+ ComboBox2->Selected->Text;
sql += " INNER JOIN " + ComboBox3->Selected->Text + " ON user_id = id" ;
sql += " WHERE ( user_name IS LIKE '%" + Edit1->Text
+ "%' OR user_surname IS LIKE '%" + Edit1->Text
+ "%' OR user_adress IS LIKE '%" + Edit1->Text + "%')";
sql += " AND ( record_date >='"+ DateEdit1->Date.FormatString("yyyy-mm-dd")
+ "' AND record_date <='" + DateEdit2->Date.FormatString("yyyy-mm-dd")+"'";
sql += " LIMIT " + ComboBox4->Selected->Text;
sql += ";";
This example is a very simple example with a second database join and some text to search, with some date formats. Note that limiting is very important, you should arrange this limit, or let the user limit. I recommend you 25, 50, 100 line limit for international connections, national and local queries can be 500 or 1000 queries as default, more than this should be an option but warn that query may take a time. Also don’t forget to add Zzz icon to mouse or to your application that shows SQL query is in execution. Set it normal when data received from the database.
Working directly with SQL
At the end of these lines instead of printing this SQL, you can directly add this SQL to your database query and use Execute() command as below,
Please check our DATABASE section for more information about database connections, and more.