Database Guide
To update a row in a database table, you use the following
technique:
- Open a database and issue a SELECT query to retrieve the row you want to
update.
- 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.
- 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.
- 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.
Note: | To find the column names for a table, send the table the
AbtTable>>columnNames message.
|
The following sample code updates a row in a table in the SAMPLE database
as follows:
- Declares temporary variables for the connection, the database, a query
specification, the row retrieved, the updated row, and the database table
- Returns the active connection for the database manager
- Opens the SAMPLE database
- Defines the query specification that selects a row
- Executes the query and retrieves the row
- Copies the row and changes the information
- 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: '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: 20000;
yourself.
table := (connection openTableNamed: 'STAFF')
atRow: oldRow putRow: newRow;
yourself.
^table asStrings.
[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]