使用已知的 db 版本初始化数据库 - 创建表
为了触发 upgradeneeded
事件,你需要使用高于当前版本的版本请求数据库 - 否则将不会触发事件。
function createTable(dbName, dbversion, tableName) {
var request = indexedDB.open(dbName, dbversion);
request.onupgradeneeded = function (e) {
var database = e.target.result;
var objectStore = database.createObjectStore(tableName, {
keyPath: 'id'
});
console.log("Object Store Created");
};
request.onsuccess = function (e) {
var database = e.target.result;
//code to verify that the table was created
database.objectStoreNames.contains(storeName);
database.close();
}
request.onerror = function (e) {
console.error(e.target.error.message);
}
}