javascript 如何在 IndexedDB 中创建多个对象存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20097662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create multiple object stores in IndexedDB
提问by user1598696
I don't know if I'm right or wrong. But as I know I can't create a version change transaction manually. The only way to invoke this is by changing the version number when opening the indexed DB connection. If this is correct, in example1 and example2 new objectStore will never be created?
我不知道我是对还是错。但据我所知,我无法手动创建版本更改事务。调用它的唯一方法是在打开索引数据库连接时更改版本号。如果这是正确的,在 example1 和 example2 中将永远不会创建新的 objectStore?
Example1
示例 1
function createObjectStore(name){
var request2 = indexedDB.open("existingDB");
request2.onupgradeneeded = function() {
var db = request2.result;
var store = db.createObjectStore(name);
};
}
Example2
例2
function createObjectStore(name){
var request2 = indexedDB.open("existingDB");
request2.onsuccess = function() {
var db = request2.result;
var store = db.createObjectStore(name);
};
}
Example3 - This should work:
示例 3 - 这应该有效:
function createObjectStore(name){
var request2 = indexedDB.open("existingDB", 2);
request2.onupgradeneeded = function() {
var db = request2.result;
var store = db.createObjectStore(name);
};
}
If I want to create multiple objectStore's in one databasehow can I get/fetch database version before opening the database?? So is there a way to automate this process of getting database version number??
如果我想在一个数据库中创建多个 objectStore,如何在打开数据库之前获取/获取数据库版本?那么有没有办法自动化这个获取数据库版本号的过程?
Is there any other way to create objectStore other than that using onupgradeneeded event handler.
除了使用 onupgradeneeded 事件处理程序之外,还有其他方法可以创建 objectStore。
Please help. Thanks a lot.
请帮忙。非常感谢。
Edit:
编辑:
Here is same problem that I have: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-html5/0rfvwVdSlAs
这是我遇到的同样问题:https: //groups.google.com/a/chromium.org/forum/#!topic/chromium-html5/0rfvwVdSlAs
回答by Deni Spasovski
You need to open the database to check it's current version and open it again with version + 1
to trigger the upgrade.
您需要打开数据库以检查它的当前版本,然后再次打开它version + 1
以触发升级。
Here is the sample code:
这是示例代码:
function CreateObjectStore(dbName, storeName) {
var request = indexedDB.open(dbName);
request.onsuccess = function (e){
var database = e.target.result;
var version = parseInt(database.version);
database.close();
var secondRequest = indexedDB.open(dbName, version+1);
secondRequest.onupgradeneeded = function (e) {
var database = e.target.result;
var objectStore = database.createObjectStore(storeName, {
keyPath: 'id'
});
};
secondRequest.onsuccess = function (e) {
e.target.result.close();
}
}
}
回答by Kristof Degrave
The only way you can create an object store is in the onupgradeneeded event. You need a version_change transaction to be able to change the schema. And the only way of getting a version_change transaction is through a onupgradeneeded event.
创建对象存储的唯一方法是在 onupgradeneeded 事件中。您需要一个 version_change 事务才能更改架构。获取 version_change 事务的唯一方法是通过 onupgradeneeded 事件。
The only way to trigger the onupgradeneeded event is by opening the database in a higher version than the current version of the database. The best way to do this is keeping a constant with the current version of the database you need to work with. Every time you need to change the schema of the database you increase this number. Then in the onupgradeneeded event, you can retrieve the current version of the database. With this, you can decide which upgrade path you need to follow to get to the latest database schema.
触发 onupgradeneeded 事件的唯一方法是在比当前数据库版本更高的版本中打开数据库。做到这一点的最佳方法是与您需要使用的数据库的当前版本保持一致。每次需要更改数据库的架构时,都会增加此数字。然后在 onupgradeneeded 事件中,您可以检索数据库的当前版本。有了这个,您可以决定需要遵循哪个升级路径才能获得最新的数据库架构。
I hope this answers your question.
我希望这回答了你的问题。