在提交之前取消交易或部分交易?

时间:2012-11-09 05:37:17

标签: sqlite commit

我有一个很长的事务,我已经开始使用BEGIN,我在最后一行之前的3行犯了一个错误。有没有办法使该行无效?

以下是一个例子:

BEGIN TRANSACTION;
CREATE TABLE purchase(id integer PRIMARY KEY, purchasePrice integer CHECK(purchasePrice>0), customer text);
INSERT INTO purchase(purchasePrice, customer) VALUES(1200, "Jerome");
INSERT INTO purchase(purchasePrice, customer) VALUES(200, "Jeffery");
INSERT INTO purchase(purchasePrice, customer) VALUES(40, "Craig");
INSERT INTO purchase(purchasePrice, customer) VALUES(1640, "Donovan");
INSERT INTO purchase(purchasePrice, customer) VALUES(100, "Williams");
INSERT INTO purchase(purchasePrice, customer) VALUES(50, "Matsushi");
INSERT INTO purchase(purchasePrice, customer) VALUES(150, "Xhao");
INSERT INTO purchase(purchasePrice, customer) VALUES(250, "Dilberta");
INSERT INTO purchase(purchasePrice, customer) VALUES(840, "Brow"); //Supposed to be 9440,"Brown"
INSERT INTO purchase(purchasePrice, customer) VALUES(440, "DarkBrown");
INSERT INTO purchase(purchasePrice, customer) VALUES(20, "Black");
COMMIT;

我明白我以后可以更新受影响的行。

另一个例子情况:

BEGIN TRANSACTION;
CREATE TABLE Movies(MId integer primary key autoincrement, Title text);
INSERT INTO Movies VALUES(1,'Capote');
INSERT INTO Movies VALUES(2,'Scent of a woman');
INSERT INTO Movies VALUES(3,'Stigmata');
INSERT INTO Movies VALUES(4,'Exorcist');
INSERT INTO Movies VALUES(5,'Hamsun');
//Changed my mind and want to name the table "movie" and  name the columns "m_id" and "title".

所以,我想知道a)如何取消;和b)是否可以上几行,如果是这样,怎么样?

2 个答案:

答案 0 :(得分:1)

如果您创建了savepoints,则可以回滚部分交易。 (但这会更加打字。)

当您进行许多更改时,最好在执行之前将所有SQL命令写入文本文件。

答案 1 :(得分:0)

ROLLBACK功能会取消整个交易。我没有找到一种逐行返回的方法,因此UPDATEALTER命令可以分别用于表或字段/行。