Learn How To Send Arrays To A PostgreSQL Database Server Using FireDAC In Delphi On Windows

Location

You can find the Arrays project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesDBMS SpecificPostgreSQLArrays
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

How to Use the Sample

  1. Navigate to the location given above and open PGArrays.dproj.
  2. Press F9 or choose Run > Run.

Files

File in Delphi Contains
PGArrays.dproj
PGArrays.dpr
The project itself.
fMain.pas
fMain.fmx
The main form.

Implementation

Before running the sample, the main components are configured at design time using the Object Inspector as follows:

  • TFDConnection object named FDConnection1. This is the FireDAC connection object that the sample uses to connect to a DBMS. The sample sets the ConnectionDefName property to PG_Demo.
  • TFDQuery object named FDQuery1. This component implements a dataset capable of executing SQL queries. The sample sets its Connection property to FDConnection1 to specify the FireDAC connection object.
  • TDataSource object named DataSource1. This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the dataset and the grid where the dataset is displayed. To this end, the sample sets the following properties:
  • The DataSet property of DataSource is set to FDQuery1.
  • The DataSource property of DBGrid1 is set to DataSource1.

When you run the application, you see a grid, a combo box and two buttons labeled as PG Read and PG Write. The purpose of these components in this sample is the following:

  • TDBGrid – This sample uses the grid to display the arrays.
  • TComboBox – Use the combo box to choose whether to define the array as ftArray or ftDataSet.
  • TButtons – Both buttons have an OnClick event to do the following:Uses the Open method to read the arrays from the database server. The sample displays the arrays on the grid.Press this button to send arrays to a database server. When you press this button, the sample takes the following steps:
    1. Uses the Text property of SQL to set the SQL command that FDQuery1 will execute.
    2. Sets the TFDParam.DataTypeName to specify the field name as ..
    3. Sets the TFDParam.ArrayType property to atTable.Note: If you set it to atArray it does not work.
    4. Sets the array size with the TFDParam.ArraySize property.
    5. Sets the arrays using the AsStrings property:
    6. Sends the arrays to the database server by executing the SQL command specified in the first step.

For more information you can refer to the link below:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.PostgreSQL_Arrays_Sample

Check out the demo source code for PostgreSQL Arrays in Windows apps here.