Products & Services Solutions Academia Support User Community Company

Learn more about Database Toolbox   

Replacing Existing Data in Databases with Data Exported from the MATLAB Workspace

This example updates the date field that you previously imported into the Avg_Freight_Cost table using the following Database Toolbox functions:

For more information on these functions, see matlab\toolbox\database\dbdemos\dbupdatedemo.m.

  1. If you have completed the previous example, skip this step. Otherwise, enter the following commands:

    conn = database('SampleDB', '', '');
    colnames = {'Calc_Date', 'Avg_Cost'};
    D = '20-Jan-2002';
    meanA = 36.6067;
    exdata = {D, meanA}
    exdata = 
    	'20-Jan-2002'    [36.6067]
    
  2. Change the date in the Avg_Freight_Cost table from 20-Jan-2002 to 19-Jan-2002:

    D = '19-Jan-2002'
    
  3. Assign the new date value to the newdata cell array.

    newdata(1,1) = {D}
    newdata =
        '19-Jan-2002'
    
  4. Specify the record to update in the database by defining a SQL where statement and assigning it to the variable whereclause. The record to update is the record whose Calc_Date is 20-Jan-2002. Because the date string is within a string, it is embedded within two single quotation marks rather than one.

    whereclause = 'where Calc_Date = ''20-Jan-2002'''
    whereclause =
    	where Calc_Date = '20-Jan-2002'
    
  5. Export the data, replacing the record whose Calc_Date is 20-Jan-2002.

    update(conn,'Avg_Freight_Cost',colnames,newdata,whereclause)
  6. In Microsoft Access, view the Avg_Freight_Cost table to verify the results.

  7. Close the cursor and disconnect from the database.

    close(conn)
    

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS