Database Guide


Using database classes for scripts

You can use the database query classes illustrated in the previous section to write VisualAge scripts that execute queries using information provided in a visual part.

The following example shows how to incorporate the sample code in Updating rows in a table into a VisualAge script. To use this code in a script, follow these steps:

  1. Create a visual part with a text field, a label, a push button, and a multi-line editor as follows:
    frdbview
  2. Switch to the script editor and create a new method called executeQuery. Paste the following script into the new method template:
    executeQuery
         "Script to Update a row in a table"
          | connection querySpec oldRow newRow table|
         connection := AbtDbmSystem activeDatabaseConnectionWithAlias: 'SAMPLE'.
          querySpec := (AbtQuerySpec new)
                              statement: 'SELECT * from STAFF
                                               where STAFF.NAME = 'Sanders' ';
                              hostVarsShape: (nil).
          oldRow := (connection resultTableFromQuerySpec: querySpec) first.
          newRow := (oldRow deepCopy)
                               at: 'SALARY'
                               put: ((self subpartNamed: 'Text1') object);
                               yourself.
          table := (connection openTableNamed: 'STAFF')
                           atRow: oldRow putRow: newRow;
                           yourself.
          ^table asStrings.
    
  3. Note that the only difference between this sample code and the sample code in Updating rows in a table is the substitution of the object attribute of the text field for the hard-coded 20000 and the inclusion of a method name, executeQuery.
  4. Save the script and return to the Composition Editor.
  5. Make the following connections:
  6. Now test the visual part as follows:
    1. Type a number into the text field.
    2. Select the Update Salary push button.
    The multi-line editor shows the same result table shown by the sample code in Updating rows in a table. The number you typed into the text field appears in the SALARY column for Sanders.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]