Updating rows in a table
To update a row in a database table, you use the following technique:
1. Open a database and issue a SELECT query to retrieve the row you want to update.
2. Send the AbtResultTable>>first message to the query result table to return an instance of AbtRow for the row returned in the query result table.
3. Send the AbtRow>>deepCopy message to the row to return a copy of the row and send the copy the AbtRow>>at:put: message to replace the existing column data with new data.
4. Send the table the message AbtTable>>atRow:putRow: with the instance of the row to be updated and the updated copy.
The SELECT statement you issue to return the row to be updated cannot contain a join operation. You can use this technique only to update rows returned from a single table.
The following sample code updates a row in a table in the SAMPLE database as follows:
1. Declares temporary variables for the connection, the database, a query specification, the row retrieved, the updated row, and the database table
2. Returns the active connection for the database manager
3. Opens the SAMPLE database
4. Defines the query specification that selects a row
5. Executes the query and retrieves the row
6. Copies the row and changes the information
7. Replaces the original row with the updated row
To use this block of code, evaluate it using the Inspect command.
"Update rows in a table"
| connection querySpec oldRow newRow table |
connection := AbtDbmSystem activeDatabaseConnectionWithAlias: 'SampleConSpec'.
querySpec := (AbtQuerySpec new)
statement: 'SELECT * from STAFF
where STAFF.NAME = ''Sanders'' ';
hostVarsShape: (nil).
oldRow := (connection resultTableFromQuerySpec: querySpec) first.
newRow := (oldRow deepCopy) at: 'SALARY' put: 20000;
yourself.
table := (connection openTableNamed: 'STAFF')
atRow: oldRow putRow: newRow;
yourself.
^table asStrings.
Last modified date: 01/29/2015