检查这个例子:
var db = openDatabase( 'test.db', 1, '', 2*1024*1024 );
db.transaction(function(tx) {
var success = function(tx,results) {
console.log( results );
};
var error = function(tx,results) {
console.log( results );
};
// clean previous state
tx.executeSql( 'DROP TABLE IF EXISTS products', [], success, error );
tx.executeSql( 'CREATE TABLE products ( id INTEGER NULL PRIMARY KEY, name TEXT )', [], success, error );
tx.executeSql( 'CREATE UNIQUE INDEX name ON products( name )', [], success, error );
tx.executeSql( 'INSERT INTO products( name ) VALUES ( "Lechuga" )', [], success, error );
tx.executeSql( 'INSERT INTO products( name ) VALUES ( "Naranja" )', [], success, error );
tx.executeSql( 'INSERT INTO products( name ) VALUES ( "Naranja" )', [], success, error );
tx.executeSql( 'INSERT INTO products( name ) VALUES ( "Tomate" )', [], getAll, error );
} );
function getAll(tx, results) {
db.transaction(function(tx) {
tx.executeSql( 'SELECT * FROM products', [], function(tx, results) {
console.assert( results.rows.length === 0 ) // false, why?
} );
} );
}
jsfiddle 链接:http: //jsfiddle.net/aBx7E/6/
触发唯一约束的插入查询,不要停下来让 websql 继续执行下一个查询。因此,在流程结束时,您将有 4 行,这是没有意义的,因为如果我处于事务状态,当触发约束错误时,表上的所有先前更改都将被丢弃。
为什么Websql不触发回滚机制?