PgDAC

Retrieving Data From Tables

This tutorial describes how to retrieve data from a table using the TPgQuery and TPgTable components.

  1. Requirements
  2. General Information
  3. TPgQuery
  4. TPgTable
  5. Additional Information

Requirements

This tutorial assumes that you have already connected to the server (see Connecting to PostgreSQL), created the necessary database objects (see Creating Database Objects), and inserted data into tables (see Inserting Data Into Tables). To retrieve data at runtime, add the PgAccess unit to the uses clause for Delphi or include the PgAccess.hpp header file for C++ Builder.

General Information

PgDAC provides the TPgQuery and TPgTable components for retrieving data from a table. This tutorial shows you how to retrieve data from the dept table.

TPgQuery

The following code demonstrates how to retrieve data from the dept table using TPgQuery:

Delphi

var
  PgQuery1: TPgQuery;
begin
  PgQuery1 := TPgQuery.Create(nil);
  try
    // PgConnection1 was set up earlier
    PgQuery1.Connection := PgConnection1; 
    // adds a statement to retrieve data
    PgQuery1.SQL.Text := 'SELECT * FROM dept';
    // opens the dataset
    PgQuery1.Open;
    // shows the number of records in the dataset
    ShowMessage(IntToStr(PgQuery1.RecordCount));
  finally
    PgQuery1.Free;
  end;
end;

C++Builder

TPgQuery* PgQuery1 = new TPgQuery(NULL);
try {
    // PgConnection1 was set up earlier
    PgQuery1->Connection = PgConnection1;
    // adds a statement to retrieve data
    PgQuery1->SQL->Text = "SELECT * FROM dept";
    // opens the dataset
    PgQuery1->Open();
    // shows the number of records in the dataset
    ShowMessage(IntToStr(PgQuery1->RecordCount)); 
}
__finally {
    PgQuery1->Free();
}

TPgTable

The following code demonstrates how to retrieve data from the dept table using TPgTable:

Delphi

var
  PgTable1: TPgTable;
begin
  PgTable1 := TPgTable.Create(nil);
  try
    // PgConnection1 was set up earlier
    PgTable1.Connection := PgConnection1; 
    // indicates the name of the table
    PgTable1.TableName := 'dept';
    // opens the dataset
    PgTable1.Open;
    // shows the number of records in the dataset
    ShowMessage(IntToStr(PgTable1.RecordCount)); 
  finally
    PgTable1.Free;
  end;
end;

C++Builder

TPgTable* PgTable1 = new TPgTable(NULL);
try {
    // PgConnection1 was set up earlier
    PgTable1->Connection = PgConnection1; 
    // indicates the name of the table
    PgTable1->TableName = "dept";
    // opens the dataset
    PgTable1->Open();
    // shows the number of records in the dataset
    ShowMessage(IntToStr(PgTable1->RecordCount));
}
__finally {
    PgTable1->Free();
}

Additional Information

It is also possible to use stored procedures to delete data, in which case all data manipulation logic is defined on the server. See Using Stored Procedures for more information.

© 1997-2024 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback