Replace Existing Data in Database

This example shows how to update a value of the month column in the table yearlysales using the data source named dbdemo. To access the example where you import the values of the month column, see Insert Data into Database Table.

To access the code for this example, see matlab\toolbox\database\dbdemos\dbupdatedemo.m.

Create a database connection conn to the Microsoft® Access™ database using the ODBC driver. Here, this code assumes that you are connecting to a data source named dbdemo with blank user name and password.

conn = database('dbdemo','','');

To update the month, specify the month column that contains the months in the cell array colnames.

colnames = {'month'};

Assign the month value March2010 to the MATLAB® variable data for the update. The data type of data is a table.

data = table({'March2010'},'VariableNames',{'month'});

Specify the record to update in the database by defining an SQL WHERE statement whereclause. The record to update is the record whose month is March. Embed March in two single quotation marks so that MATLAB interprets March as a character vector in the SQL WHERE statement.

whereclause = 'WHERE month = ''March'''
whereclause =

	'WHERE month = 'March''

Update the data for the record whose month is March in the database table yearlysales.

update(conn,'yearlysales',colnames,data,whereclause)

In Microsoft Access, view the yearlysales table to verify the results.

Close the database connection.

close(conn)

See Also

| |

Related Topics

External Websites