User`s guide

6 Using Database Toolbox Functions
6-16
Delete Data from Databases
This example shows how to delete data from your database using MATLAB.
Create the SQL string with your deletion SQL statement. Consult your database
documentation for the correct SQL syntax. Execute the delete operation on your database
using exec with your SQL string. This example demonstrates deleting data records in a
Microsoft Access database.
Connect to the Database
Connect to the Microsoft Access database using native ODBC and the data source name
dbtoolboxdemo. This database contains the table inventoryTable with the column
productNumber.
conn = database.ODBCConnection('dbtoolboxdemo','','');
Display the last rows in the table inventoryTable.
curs = exec(conn,'select * from inventoryTable');
curs = fetch(curs);
curs.Data
ans =
...
[11] [ 567] [ 0] '2012-09-11 00:30...'
[12] [1278] [ 0] '2010-10-29 18:17...'
[13] [1700] [14.5000] '2009-05-24 10:58...'
Delete a Specific Record
Delete the data for the product number 13 from the table inventoryTable. Specify the
product number using the WHERE clause in the SQL statement.
curs = exec(conn,'delete * from inventoryTable where productNumber = 13');
Display the last rows in the table inventoryTable after the deletion.
curs = exec(conn,'select * from inventoryTable');
curs = fetch(curs);
curs.Data
ans =