本质上使用 MDN IndexedDb tutorial 中的示例我可以看到我的测试 IndexedDb 代码在 Chrome 上运行。当我在 deviceready
处理程序内将应用加载到我的 Windows Phone 8 设备上时,我在数据库打开请求的错误处理程序中收到了一个 AbortError
。
唯一的其他相关 SO 问题已通过修复 onupgradeneeded
中的错误解决,但我的代码中从未调用过此处理程序。
在这个简单的示例中,您必须运行 fiddle 两次,因为显然 onsuccess
在 onupgradeneeded
(我写入值的地方)之前被调用(我在其中读取测试值)当数据库初始化时)。一旦我的第一个测试开始工作,我就打算处理这个问题。
// 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!");
};
};
};
问题:
- 我的愚蠢错误是什么?
- 是否有任何 Windows Phone 8 的 IndexedDb 和 PhoneGap 示例?经过一番搜索,我找不到任何东西。有一些用于 IndexedDb API Android 和 IOS polyfill,但没有用于 wp8。
- 因为我正在打电话,所以我必须做些什么特别的事情吗?同样,该代码适用于 chrome。
- 是否有任何其他插件支持 wp8 存储 > 5mb?
- LocalStorage 的大小限制为 5mb
- 不支持 WebSQL
- 文件系统插件不支持 filewriter.write(blob)。这就是我在 Android/iOS 上使用的。奇怪的是,他们说这个插件支持 wp8,而
这是实际写入数据的唯一方法,你无法读取你可以写入的内容。 我发现虽然web api不支持,但是设备支持filewriter.write(string)
。 Windows Phone 8 仍然无法完全正确地写入/读取内容,但这是一个单独的问题。
最佳答案
最近,我遇到了与索引数据库类似的问题。我的 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");
};
};
关于javascript - 打开数据库时 PhoneGap Windows Phone 8 IndexedDB AbortError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659553/