此示例展示如何更新数据库中的数据并回滚更改。回滚这些更改将恢复运行更新之前显示的数据。
创建数据库连接康涅狄格州
.例如,下面的代码使用数据库toy_store
、用户名用户名
、密码松材线虫病
,服务器名称sname
,端口号123456
连接到微软®SQL Server®数据库。这个数据库包含这个表inventoryTable
包含以下列:productNumber
,数量
,价格
.
康涅狄格州=数据库(“toy_store”,“用户名”,“pwd”,...“供应商”,“Microsoft SQL Server”,...“服务器”,“sname”,...“PortNumber”, 123456);
设置自动提交
财产的连接
对象“关闭”
.关闭此标志后进行的任何更新都不会自动提交到数据库。
conn.AutoCommit =“关闭”;
中显示数据inventoryTable
表更新之前。方法从执行的查询导入数据获取
函数,并显示导入数据的前几行。
d =获取(康涅狄格州,'SELECT * FROM inventoryTable');头(d)
ans = 8×4表productnumber inventorydate数量价格 _____________ ________ _____ _____________________ 1 1700 14.5 2014-09-23 09:38:34的2 1200 9 2014-07-08 22:50:45的3 356 17 2014-05-14 07:14:28 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”
为第一个产品的新价格定义一个单元格数组。
数据(1,1)= {30.00};
定义在哪里
第一个产品的条款。
whereclause ='where productNumber = 1';
更新价格
列inventoryTable
对于第一个产品。
表名=“inventoryTable”;colname = {“价格”};表,更新(康涅狄格州colname、数据whereclause)
中显示数据inventoryTable
更新后的表。
d =获取(康涅狄格州,'SELECT * FROM inventoryTable');头(d)
ans = 8×4表productnumber inventorydate数量价格 _____________ ________ _____ _____________________ 1 1700 30 2014-09-23 09:38:34的2 1200 9 2014-07-08 22:50:45的3 356 17 2014-05-14 07:14:28 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-1811:45:35'
第一个产品的更新价格为30.
.尽管更新了数据,但更改还没有提交到数据库。
回滚更新。
回滚(康涅狄格州)
或者,您可以使用SQL回滚更新回滚
语句,使用执行
函数。
中显示数据inventoryTable
回滚更新后的表。
d =获取(康涅狄格州,'SELECT * FROM inventoryTable');头(d)
ans = 8×4表productnumber inventorydate数量价格 _____________ ________ _____ _____________________ 1 1700 14.5 2014-09-23 09:38:34的2 1200 9 2014-07-08 22:50:45的3 356 17 2014-05-14 07:14:28 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”
第一个产品的旧价格是14.50
.
关闭数据库连接。
关上(康涅狄格州)