0

尝试使用 JavaScript 同步打开数据库。我的代码看起来像这样。每当我在 Chrome 上运行它时,我都会收到以下 2 个错误

未捕获的 SecurityError:无法构造“Worker”:无法从源“null”访问“file:///C:/Users/KH1483/Desktop/web%20sql/worker.js”处的脚本。dbSync.html:23

未捕获的类型错误:无法读取未定义的 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']);
}

} 
}); 
4

0 回答 0