基本上使用MDN IndexedDb tutorial中的示例我可以看到我的测试IndexedDb代码正在Chrome上运行。当我将应用程序加载到deviceready
处理程序内的Windows Phone 8设备上时,我在数据库打开请求的错误处理程序中得到AbortError
。
通过修复onupgradeneeded
中的错误解决了唯一的其他相关SO问题,但我的代码中甚至都没有调用此处理程序。
在这个简单的例子中,你必须运行两次小提琴,因为在onsuccess
之前调用了onupgradeneeded
(在那里我读了一个测试值)(我在db初始化时写了值) 。一旦我完成了第一次测试,我就会处理这个问题。
// In the following line, you should include the prefixes of
// implementations you want to test.
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.");
}
// open the database
var db;
var request = window.indexedDB.open("MyTestDatabase", 1);
request.onerror = function(e) {
alert("Couldn't open database: " + kc.util.getObjectString(e.target));
};
request.onsuccess = function(e) {
db = e.target.result;
var getRequest =
db.transaction("data")
.objectStore("data")
.get("firstObject")
.onsuccess = function(event) {
alert("Got: " + event.target.result.test);
};
};
request.onupgradeneeded = function(e) {
var db = event.target.result;
var objectStore = db.createObjectStore("data", {
autoincrement : false
});
objectStore.transaction.oncomplete = function(event) {
var myObjectStore = db.transaction("data", "readwrite").objectStore("data");
var addRequest = myObjectStore.add({
test : true
},
"firstObject");
addRequest.onerror = function(e) {
console.log("Error adding");
};
addRequest.onsuccess = function(e) {
console.log("Added!");
};
};
};
问题:
filewriter.write(string)
。 Windows Phone 8仍然没有完全正确地编写/阅读内容,但这是一个单独的问题。 答案 0 :(得分:1)
最近,我遇到了索引数据库的类似问题。我的IndexedDB.open请求抛出了一个中止错误。
以下是供参考的代码。
function create_db(db_name)
{
var request = indexedDB.open(db_name);
request.onupgradeneeded=function(e)
{
console.log("1. creating database");
db=e.target.result;
};
request.onsuccess = function(e)
{
db = e.target.result;
console.log("1.1 database created successfully");
db.close();
add_tables(db_name);
};
request.onerror=function(e)
{
alert("error: "+ e.target.error.name + "failed creating db");
console.log("1.2 error creating db");
};
}
function add_tables(db_name)
{
var request = indexedDB.open(db_name,2);
request.onsuccess=function(e)
{
db=e.target.result;
console.log("2.2 table creation request successful");
};
request.onupgradeneeded=function(e)
{
db=e.target.result;
table = db.createObjectStore("table_name");
table.createIndex("id","id");
console.log("2.2 creating a single object store");
};
request.onerror=function(e)
{
console.log("2.3 error occured when creating tables");
};
};
答案 1 :(得分:0)
只是一些想法,希望他们帮助: