In the database management system, the main methods of query are as follows: 1. A single query for a fixed field. 2. A single query that can select fields. 3. Limit multiple queries for several fields. 4. Multiple queries that can be arbitrarily selected fields. The first two are also called single condition queries, and the latter two are called multiple (or multiple-condition) queries.
In practice, the system (which is actually a programmer) provides the user with a query that is more than a single condition query, and even if it provides a multiple-conditional approach, there are usually only two or three conditions, since writing multiple queries is a tricky and cumbersome thing. In fact, multiple queries can be implemented easily with tabular grid functionality. I take Delphi as an example, introduce the concrete realization method. This idea is also appropriate for other programming languages (such as visual Foxpro).
The program is mainly implemented as follows: three functions.
Set DBGrid;
Generate query criteria (statements);
Execute the query.
The specific steps are as follows:
⑴ a new project file, named PDBGRID.DPR;
⑵ Name the unit file as Udbgrid.pas, add the following controls in its corresponding form (named Frmdbgrid) and write the appropriate code:
The structure of the table Cxcomm.db Table2 (the dataset that records the query criteria) is defined as follows:
The Formactivate event code for the form is as follows:
procedure TfrmDBGrid.FormActivate(Sender: TObject);
begin
if Table2.Active then Table2.Close;
Table2.EmptyTable; //清空条件
Table1.Open;
Table2.Open;
end;
Button1(设置DBGrid)的Click事件代码如下:
procedure TfrmDBGrid.Button1Click(SenderTObject);
begin
MySetDBGrid(Table1,DBGrid2);
// MySetDBGrid为自定义过程
//以Table1为数据源,DBGrid2为记录筛选(查询)条件的表格
end;
The Click event code for the Button2 (build query) is as follows:
procedureTfrmDBGrid.Button2Click(Sender:TObject);
begin
if MyCreate_SQL(Table1,Table2,Query1)
//MyCreate_SQL为自定义函数
//由指定数据来源表生成SQL,存入Query1
then begin
Memo1.Lines.Clear;
Memo1.Lines:=(Query1.SQL);
Memo1.Modified:=false;
end
end;