This tutorial describes how to retrieve data from tables using the TMyQuery and TMyTable components.
This walkthrough supposes that you know how to connect to server (tutorials "Connecting To MySQL" and "Connecting To MySQL Embedded Server"), how to create necessary objects on the server (tutorial "Creating Database Objects"), and how to insert data to created tables (tutorial "Inserting Data Into Tables").
As we know, an original function of any database application is establishing connection to a data source and working with data contained in it. MyDAC provides several components that can be used for data retrieving, such as TMyQuery and TMyTable. We will discuss data retrieving using these components.
The goal of this tutorial is to retrieve data from a table dept.
The following code demonstrates retrieving of data from the dept table using the TMyQuery component:
[Delphi]
var
q: TMyQuery;
begin
q := TMyQuery.Create(nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
q.Connection := con;
// retrieve data
q.SQL.Text := 'SELECT * FROM dept';
q.Open;
// shows the number of records obtained from the server
ShowMessage(IntToStr(q.RecordCount));
finally
q.Free;
end;
end;
[C++Builder]
{
TMyQuery* q = new TMyQuery(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
q->Connection = con;
// retrieve data
q->SQL->Text = "SELECT * FROM dept";
q->Open();
// shows the number of records obtained from the server
ShowMessage(IntToStr(q->RecordCount));
}
__finally
{
q->Free();
}
}
The following code demonstrates retrieving of data from the dept table using the TMyTable component:
[Delphi]
var
tbl: TMyTable;
begin
tbl := TMyTable.Create(nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
tbl.Connection := con;
// retrieve data
tbl.TableName := 'dept';
tbl.Open;
// shows the number of records obtained from the server
ShowMessage(IntToStr(tbl.RecordCount));
finally
tbl.Free;
end;
end;
[C++Builder]
{
TMyTable* tbl = new TMyTable(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
tbl->Connection = con;
// retrieve data
tbl->TableName = "dept";
tbl->Open();
// shows the number of records obtained from the server
ShowMessage(IntToStr(tbl->RecordCount));
}
__finally
{
tbl->Free();
}
}
It is also possible to use stored procedures for data retrieving. In this case, all data manipulation logic is defined on server. You can find more about using stored procedures in the tutorial "Stored Procedures".