[PB] solves text garbled during dynamic generation of data windows

Source: Internet
Author: User

In practice, we often encounter tables in the database that are dynamically generated during the running of the program. To operate the data in such tables, we need to use dynamic data window objects, it is worth mentioning that when a dynamic data window object is generated, the text in its header band cannot be directly displayed as Chinese characters, which is indeed a pity, however, this problem can be solved through the ing method.
I. Question proposal
There are two tables:
The records in Table A can be freely added, modified, deleted, and updated during the running of the program. The attributes (I .e., fields) in table B are determined by the records in Table A and dynamically created, furthermore, it provides a hand-written operation interface for data in table B. Of course, Create Table B as the following table
Yes. However, when a data window object is dynamically generated, the text in the header band is not displayed as Chinese characters, but garbled, which is not conducive to solving the problem.
Ii. Problem Solving
1. Dynamic table B creation Solution
To dynamically create table B, we need to build dynamic SQL statements. What is a dynamic SQL statement? Database applications usually perform definite work. Therefore, when writing and compiling, you can determine the complete SQL statement, however, when you need to embed SQL statements (such as DDL statements) that are not supported by PowerBuilder, or do not know the specific format or parameters of the statements during compilation, an SQL statement is formed at runtime, these statements are called dynamic SQL statements.
Dynamic SQL statement format:
Execute immediate SQL statement [using transation object];
Parameter description:
SQL statement contains a string with SQL statements.
Transation Object Transaction object
2. Dynamic Data window object creation and text localization in Header band
(1) When the program is running, we can call the create function to dynamically generate a data window object. The format is as follows:
DataWindowControl. Create (Syntax {, Errorbuffer })
Parameter description:
DataWindowControl: name of the data window control in which the data window object is created
Source code of the Syntax data window object
The Errorbuffer string used to save the error message.
(2) because the name of the text box in the Header Band is composed of the name of the corresponding column in the corresponding Detail Band with "_ t", and the Detail Band) the column name is the name of the corresponding table attribute. Therefore, we can use Cursor to retrieve the values of ywm and zwm in Table A, and then replace the Text attribute of the Text box in Header Band by ing, in this way, the text in the text box in the Header Band is displayed as the corresponding Chinese characters.
3. A simple example
In the environment of PowerBuilder 6.5 and ms SQL Server 6.5, a simple example is used to implement the above viewpoint. The article only lists the main interfaces and key programs.
1. The code corresponding to the "dynamically create table B" button is:
Int li_ I = 1, li_count
String ywm [], zdlx [], ls_temp, ls_ SQL
// Use Cursor to obtain the values of ywm and zdlx from the database table and store them in arrays ywm [] and zdlx [].
Select count (*) into: li_count from;
DECLARE a_cur cursor for select ywm, zdlx FROM a order by ywm;
OPEN a_cur;
Fetch a_cur into: ywm [li_ I],: zdlx [li_ I];
Do While sqlca. sqlcode = 0
Ls_temp = trim (zdlx [li_ I])
Choose case ls_temp
Case "1"
Ls_temp = "char (10 )"
Case "2"
Ls_temp = "numeric (7, 2 )"
End choose
Zdlx [li_ I] = ls_temp
Ywm [li_ I] = trim (ywm [li_ I])
Li_ I ++
Fetch a_cur into: ywm [li_ I],: zdlx [li_ I];
Loop
Close a_cur;
// Create Table B and primary key (primary key)
Ls_ SQL = "Create Table B" + "(" + ywm [1] + "" + zdlx [1] + "not null ,"&
+ "Constraint pk_ B primary key (" + ywm [1] + "))"
Execute immediate: ls_ SQL;
// Add columns in Table B
For li_ I = 2 to li_count
Ls_ SQL = "alter table B add" + ywm [li_ I] + "" + zdlx [li_ I]
Execute immediate: ls_ SQL;
End
2. The code corresponding to the "dynamically create data window object" button is:
String syntax, sqlselect, errmsg, ls_col1, ls_col2, ls_name, ls_ B;
// Dynamically create a data window object DataWindow
Ls_ B = "B"
Sqlselect = "select * from" + ls_ B;
Syntax = sqlca. syntaxfromsql (sqlselect, "style (type = grid)", errmsg );
Dw_1.create (syntax)
// The Header Band text
DECLARE v_cur cursor for select ywm, zwm FROM a order by ywm;
OPEN v_cur;
FETCH v_cur INTO: ls_col1,: ls_col2;
Do while sqlca. sqlcode = 0
Ls_name = ls_col1 + '_' + 'T' + '.' + 'text' + '=' + '"' + ls_col2 + '"'
Dw_1.modify (ls_name)
FETCH v_cur INTO: ls_col1,: ls_col2;
Loop
Close v_cur;
Dw_1.settransobject (sqlca );
Dw_1.retrieve ()
Iv. Conclusion
Using PowerBuilder's dynamic data window object technology application can solve the problem of dynamic data processing in the actual database, so that the application can have better interaction and adaptability, it is widely used.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.