C++Builder Learn To Use PostgreSQL Database Connections With C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn To Use PostgreSQL Database Connections With C++
March 5, 2021 By Yilmaz Yoru

Для просмотра ссылки Войди или Зарегистрируйся is another popular database which is powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance You can Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся PostgreSQL through the Для просмотра ссылки Войди или Зарегистрируйся. They have also support with Для просмотра ссылки Войди или Зарегистрируйся, it provides many helpful places to become familiar with the technology, discover how it works, and find career opportunities. Для просмотра ссылки Войди или Зарегистрируйся.

Для просмотра ссылки Войди или Зарегистрируйся 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
1615028143365.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 Для просмотра ссылки Войди или Зарегистрируйся 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 PostgreSQL database with a table and you have installed FireDAC Components in your Component Palette, Let’s see steps to connect to a PostgreSQL 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 PostgreSQL server, we need 4 FireDAC Components . Drag FDConnection (TFDConnection), DataSource (TDataSource) , FDQuery (TFDQuery), FDCommand (TFDCommand) components on to your Form (TForm).

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

4. Double click to FDConnection component to enter postgreSQL server connection details.

Enter parameters as below;
1615028166075.png
Driver ID:
We will connect to PostgreSQL database, so Driver ID should be PG
Server:
If you connect to a remote PostgreSQL Server type its PostgreSQL IP or PostgreSQL address
If you connect to a server on your computer- type localhost or your computer IP address
Port:
It is generally 5432, if you or your host setup a different port number check your port number from PostgreSQL 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.

5. Now you can directly connect in C++ Builder with this component by using FDConnection1 component property as below;
C++:
FDConnection1->Connected=true;
You can check if it is connected by if clauses as below
C++:
if(FDConnection1->Connected)
{
    // Open a Table or tables and do some other commands
}
else
{
   // Show message that you can't connect, may be use has wrong username or password
}
For example you can do this on a button as below;
C++:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 if(DM->FDConnection1->Connected)
 {
 DM->FDConnection1->Disconnect();
 Button1->Text=u"Connect";
 }
 else
 {
 DM->FDConnection1->Connect();
 Button1->Text=u"Disconnect";
 }
}
6. PostgreSQL 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
SQL:
select * from mytable;
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;
C++:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 DM->FDQuery1->SQL->Clear();
 DM->FDQuery1->SQL->Add("Select * from mytable;");
 DM->FDQuery1->Open();
}
Full video about this application FireDAC can be found here Для просмотра ссылки Войди или Зарегистрируйся