尝试使用JavaScript同步打开数据库。我的代码看起来像这样。每当我在Chrome上运行它时,我都会收到以下2个错误
Uncaught SecurityError:无法构造'Worker':无法从origin'null'访问'file:/// C:/Users/KH1483/Desktop/web%20sql/worker.js'中的脚本。 dbSync.html:23
未捕获的TypeError:无法读取未定义的dbSync.html:16
的属性“行”
<html>
<head>
<script>
function test() {
db = openDatabase("Foo", "", "", 1);
if (db)
alert("Opened Foo");
else
alert(2);
db = openDatabase("WorkerFoo", "", "", 1);
var query='select * from test';
db.transaction(function(tx){
result=tx.executeSql(query);
if(result.rows.length>0){
for(var l=0; l<result.rows.length; l++){
alert('Result: '+result.rows.item(l)['id']);
}
}
});
worker = new Worker('worker.js');
worker.onmessage = function(event) {
alert(event.data);
}
}
</script>
</head>
<body onload="test()">
</body>
</html>
/ * Worker.js从这里开始* /
db = openDatabaseSync("WorkerFoo", "", "", 1);
if(db) postMessage('Web Worker DB Synchronous Built');
try {
var query='create table if not exists test (id int(10))';
db.transaction( function(tx){ tx.executeSql(query, [], null,null); });
postMessage('Table Created');
}catch(err){
postMessage("Exception: " + err + "; code = " + err.code);
}
var query='insert into test (id) values (100)';
db.transaction( function(tx){ tx.executeSql(query, [], null,null); });
postMessage('Data Inserted');
var query='select * from test';
db.transaction(function(tx){
result=tx.executeSql(query);
if(result.rows.length>0){
postMessage("Data Length="+result.rows.length);
for(var l=0; l<result.rows.length; l++){
postMessage('Result: '+result.rows.item(l)['id']);
}
}
});