从数据库中删除数据
这个例子展示了如何从数据库中删除数据使用MATLAB®。
创建SQL语句删除SQL语法。请咨询您的数据库文档正确的SQL语法。执行删除操作在数据库使用执行
函数的SQL语句。这个例子演示了删除的数据记录Microsoft Access®数据库。
连接到数据库
创建数据库连接康涅狄格州
到一个Microsoft Access数据库使用ODBC驱动程序和数据源名称dbdemo
。这个数据库包含了表inventorytable
与列productnumber
。
康涅狄格州=数据库(“dbdemo”,”,”);
SQL查询sqlquery
选择表中所有行数据inventorytable
。执行SQL查询使用康涅狄格州
。导入的数据执行查询使用获取
功能和显示最后几行。
sqlquery =“SELECT * FROM inventorytable”;data =获取(康涅狄格州,sqlquery);尾(数据)
ans = 8×4表productnumber数量价格inventorydate _________________ ________ _____ _____________________ 6 4540 8的2013-12-25 19:45:00 7 6034 16 2014-08-06 08:38:00的8 8350 5的2011-06-18 11:45:35 9 2339 13 2011-02-09 12:50:59的10 723 24 2012-03-14 13:13:09 11 567 0 2012-09-11 00:30:24 12 1278 0 2010-10-29 18:17:47 13 1700 14.5“2009-05-24 10:58:59”
删除特定的记录
删除产品编号的数据13
从表中inventorytable
。指定使用的产品编号在哪里
条款的SQL语句sqlquery
。
sqlquery =“删除*从inventorytable productnumber = 13 ';execute(康涅狄格州,sqlquery)
显示表中的数据inventorytable
后删除。产品编号的记录13
不见了。
sqlquery =“SELECT * FROM inventorytable”;data =获取(康涅狄格州,sqlquery);尾(数据)
ans = 8×4表productnumber数量价格inventorydate _________________ ________ _____ _____________________ 5 9000 3 2012-09-14 15:00:25 6 4540 8 2013-12-25 19:45:00 7 6034 16 2014-08-06 08:38:00的8 8350 5的2011-06-18 11:45:35 9 2339 13 2011-02-09 12:50:59的10 723 24 2012-03-14 13:13:09 11 567 0 2012-09-11 00:30:24 12 1278 0 2010-10-29 18:17:47”
删除记录使用MATLAB变量
定义一个MATLAB变量productID
通过设置产品编号12
。
productID = 12;
使用MATLAB删除数据变量productID
。建立一个SQL语句sqlquery
结合MATLAB的SQL delete操作变量。由于变量是数字和SQL语句是一个特征向量,将数字转换成一个特征向量。使用num2str
函数的转换。连接删除SQL语句和数字转换使用方括号。
sqlquery = [“从inventorytable删除*”…“productnumber = 'num2str (productID)];execute(康涅狄格州,sqlquery)
显示表中的数据inventorytable
后删除。产品编号的记录12
不见了。
sqlquery =“SELECT * FROM inventorytable”;data =获取(康涅狄格州,sqlquery);尾(数据)
ans = 8×4表productnumber数量价格inventorydate _________________ ________ _____ _____________________ 4 2580 21 2013-06-08 14:24:33的5 9000 3 2012-09-14 15:00:25 6 4540 8 2013-12-25 19:45:00 7 6034 16 2014-08-06 08:38:00的8 8350 5的2011-06-18 11:45:35 9 2339 13的2011-02-09 12:50:59 10 723 24 2012-03-14 13:13:09 11 567 0 2012-09-11 00:30:24”
关闭数据库连接
关上(康涅狄格州)