Generate a dynamic data input form from the DB2 table on Linux
Source: Internet
Author: User
Article title: generate a dynamic data input form from the DB2 table on Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Introduction
In this article, to dynamically build views and forms, I will check®DB2®Universal Database™(UDB) metadata. In particular, I will use®Kylix 3™And Borland dbExpress data access driver to analyze DB2 UDB database tables and fields (names and types), so that users can select a specific table and switch back and forth the fields that should be displayed, and dynamically view data and the output in a separate data sensing control.
This is a pure running method; no further encoding is required. In subsequent articles, I will use Borland Delphi™This method is further described by dynamically generating forms and adding them to the project during design (using our own database form wizard.
Start from scratch
Unlike the previous article about using Kylix to operate DB2, in the previous article, you have used IDE to build the entire application at design time, so you only need to connect to the DB2 UDB database. Other steps are completed in the code, and metadata obtained from the database itself is used.
To build a connection to the DB2 udb sample database, you need to use the TSQLConnection component, open a new Kylix 3 project, and place the TSQLConnection component on the form. You can right-click the SQLConnection component to edit the connection properties. DriverName should be DB2, and Database name should be SAMPLE (or any other Database name-the code in this article will process all existing DB2 UDB databases ). You also need to specify User_Name and Password. In addition, you need to enter them in the logon dialog box at runtime. You can click the "check" button to test the validity of the connection property. then, you can see the following screenshot:
Figure 1. successfully connected DB2Connection
Design preparation
Once the TSQLConnection component is set correctly, you can add other dbExpress components and connect them to each other. However, you do not need to specify SQL statements or add any view data-aware controls. When adding the first additional component, place a TSQLDataSet component on the form, and place the TSQLConnection component when adding the next component. Point the SQLConnection attribute of TSQLDataSet to the TSQLConnection component. Then, place a TDataSetProvider on the form and point its DataSet attribute to the TSQLDataSet component.
This component is followed by the TClientDataSet component. its ProviderName attribute is set to the name of the TDataSetProvider component. Finally, delete the TDataSource component on the form and connect its DataSet attribute to the TClientDataSet component. This will provide you with a link to TSQLConnection-TSQLDataSet-TSQLDataSetProvider-TClientDataSet-TDataSource. you can see this link before using Borland dbExpress that contains the DB2 UDB database table.
This time, the main difference is that you cannot specify an SQL statement within the SQLDataSet component. You cannot place any visual data-aware components on the form, because all these operations will be completed dynamically at runtime, it is based on the metadata found in the DB2 UDB database and the selection of tables and fields.
To separate the metadata information and the options generated from the dynamic view from a data pane and a data control, you need to use the TPageControl component, as shown in the screenshot below:
Figure 2. dynamic forms during design
Meta Data TabSheet is composed of the TListBox control on the left and the TCheckListBox control on the right. The former will be filled with the table name in the DB2 udb sample database table, the latter is filled with valid field names in the selected table.
The Generate Button prepares and executes SQL statements, and puts the generated Data into a pane (on the Data Grid tab ), you can also add it to a separate Data sensing control (on the Data Controls tab ).
Retrieve TableNames
The first thing to do is to retrieve the available table names in the database. Assume that the SQLConnection component can establish a connection to the DB2 UDB Database (in this example, it is the SAMPLE database or any other database). you can only open the connection explicitly. then, you can call the GetTableNames method. This method expects to use the TStrings variable as the first parameter. Instead of declaring and using our own TStringList as the first parameter, you can directly assign the result to the Items attribute of ListBox, as shown in the following code snippet:
When creating a form (in the OnCreate event handler), the result of executing this code is: when the application is started, the TListBox control is filled with all available tables.
Analysis field definition
The next step depends on the end user. To select a specific table and display available fields, the end user must double-click the table in the ListBox control. This activates an event processing program that uses the table name in ListBox and retrieves metadata for the table. From the metadata point of view, you must use the field name list to place them in the second list box TCheckListBox.
To do this, you can use the TSQLDataSet component, set CommandType to ctTable, and set CommandText to the name of the table selected from the first TListBox control, this table can be found in ListBox1.Items [ListBox1.ItemIndex. Then, do not activate (or open) TSQLDataSet. this is very important because it will send select * from the table name you selected to the database. at this time, this operation is not what you want. You should call the TSQLDataSet. FieldDefs. Update Method. this method only defines the retrieval metadata for the field and does not retrieve the actual record data for the selected table.
With this field to define metadata, you can traverse individual field definitions and place the field names in TCheckListBox (you can also include the field type, however, this type is used only in the next step, that is, it is used only when an appropriate data sensing control is created for the selected field ).
The source code snippet for retrieving field definitions and updating TCheckListBox is as follows:
Procedure TForm1.SelectTableClick (Sender: TObject );
Var
I: Integer;
Begin
ClientDataSet1.Active: = False;
SQLDataSet1.CommandType: = ctTable;
If ListBox1.ItemIndex> = 0 then
SQLDataSet1.CommandText: = ListBox1.Items [ListBox1.ItemIndex];
SQLDataSet1.FieldDefs. Update; // get meta information
CheckListBox1.Clear;
For I: = 0 to Pred (SQLDataSet1.FieldDefs. Count) do
CheckListBox1.Items. Add (SQLDataSet1.FieldDefs [I]. Name)
End;
Note: before adding a new field name, you should clear TCheckListBox. in this way, the end user can double-click different table names consecutively to view the new field list that appears in the TCheckListBox table each time.
The following screenshot shows the effect of double-clicking the EMPLOYEE table in TListBox on the left of the form for the first time. you can check the check box to obtain the first 11 fields in the TCheckListBox table on the right of the form.
Figure 3. metadata: Table and field
The next step is to generate an SQL statement. select only the fields selected in the specified table (in this example, "EMPLOYEE"), and then display the results in the data grid and data awareness control.
Generate dynamic SQL statements
By setting the CommandType attribute of TSQLDataSet to ctQuery, you can start the OnClick event handler of the Generate button (as shown in Figure 3. In this case, you do not need to specify TableName in CommandText, but the actual SQL statement selects the check fields from the specified table. You can start by assigning SELECT to the CommandText attribute and traverse the items in the TCheckListBox control. Note: You do not need to retrieve metadata from the selected table again because the corresponding field name has been added to TCheckListBox. therefore, you can only use the field names in the control (you only need to verify that the check attribute is set to True to use each individual field name ).
The variable comma in the code snippet below is required. except for the first field, make sure that a comma is inserted before each field. This generates a valid SQL statement containing one or more fields in the selected field list.
Procedure TForm1.btnGenerateClick (Sender: TObject );
Var
I, Y: Integer;
Comma: Boolean;
Begin
ClientDataSet1.Active: = False;
SQLDataSet1.CommandType: = ctQuery;
SQLDataSet1.CommandText: = 'select ';
Comma: = False;
For I: = 0 to Pred (CheckListBox1.Items. Count) do
Begin
If CheckListBox1.Checked [I] then
Begin
If not comma then comma: = True
Else
SQLDataSet1.CommandText: = SQLDataSet1.CommandText + ',';
SQLDataSet1.CommandText: = SQLDataSet1.CommandText + CheckListBox1.Items [I]
End
End;
If ListBox1.ItemIndex> = 0 then
SQLDataSet1.CommandText: =
SQLDataSet1.CommandText + 'from' + ListBox1.Items [ListBox1.ItemIndex];
// ShowMessage (SQLDataSet1.CommandText );
After you complete the field list operation, you only need to add the FROM string, which is followed by the name of the database table. you can still find the table in the TListBox control. After completing this step, you can set the Active attribute of the TClientDataSet component to True, which activates the SQL command in the TSQLDataSet component to make the data valid.
As an example, the second page of PageControl can contain the TDBGrid control that points to the DataSource attribute of the TDataSource component, as shown in the following screenshot:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service