| Database Toolbox User's Guide | Search  Help Desk |
| update | Examples See Also |
Replace data in database table with data from MATLAB cell array
Syntax
update(conn, 'tablename', colnames, data, 'whereclause')
Description
update(conn, 'tablename', colnames, data, 'whereclause')
exports data from the MATLAB cell array, data, into the database table, tablename, via the database connection conn. It replaces existing records in the table as specified by the SQL command, whereclause. Specify the column names for tablename in the MATLAB cell array, colnames.
The status of the autocommit flag determines if update automatically commits the data or if an SQL commit command is needed. View the autocommit flag status for the connection using get and change it using set. Perform an SQL commit or rollback using exec.
To add new rows instead of replacing existing data, use insert.
Examples
Example 1 - Update a record
In theBirthdays table, update the record where First_Name is Jean, replacing the current value for Age with the new value, 40. The connection is conn.
Define a cell array containing the column name you are updating, Age:
C = {'Age'}
Define a cell array containing the new data:
B(1,1) = {40}
Perform the update:
update(conn, 'Birthdays', C, B, 'where First_Name = ''Jean''')
Example 2 - Update followed by SQL rollback
This example shows a database update when theautocommit flag is off and the data is then rolled back. First set the autocommit flag to off for database connection connect:
set(connect, 'autocommit', 'off')Update the data in the column names specified by
C of the Error_Rate table for the record selected by whereClause using data contained in the cell array data:
update(connect, 'Error_Rate', C, data, whereClause)The data was written but not committed. Roll back the data:
cursor_status = exec(connect,'rollback')The
update was reversed; the data in the table is the same as it was before update was run.
See Also
database, insert, set