<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--让代码脚本运行到各个浏览器上-->
<script>
window.indexedDB=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB;
window.IDBTransaction=window.IDBTransaction||window.webKitIDBTransaction||window.msIDBTransaction;
window.IDBKeyRange=window.IDBKeyRange||window.webKitIDBKeyRange||window.msIDBKeyRange;
window.IDBCursor=window.IDBCursor||window.webkitIDBCursor||window.msIDBCursor;
//版本更新的函数
function creatObjectStore() {
// 创建数据库名称;
var dbname="indexedDBtest";
// 创建数据库版本
var dbVersion=11;
// 做对象
var idb;
// 做链接
var dbconnect=indexedDB.open(dbname,dbVersion);
dbconnect.onsuccess=function (e) {
idb=e.target.result;
alert("数据库链接成功");
} ;
dbconnect.onerror=function () {
alert("数据库链接失败");
};
// 更新数据库:版本更新
dbconnect.onupgradeneeded =function (e) {
idb=e.target.result;
// 数据库仓库名称;
var name="user";
// 数据库参数;
var optionalParameters={
// 路径,内容;
KeyPath:"userid",
autoIncrement:false
};
// 创建仓库对象
var store=idb.creatObjectStore(name,optionalParameters);
alert("对象仓库仓库创建成功");
}
}
</script>
</head>
<body>
<!--所有对数据对操作都在事务内部执行:事务分为三种1读2读写3版本中心,-->
<!--对于创建对象仓库,索引操作只能在版本中心内部进行,对象仓库不允许在同一个版本中发生改变,-->
<!--因此创建或删除对象仓库必须使用新的版本号来更新数据库对版本-->
<input type="button" value="创建仓库" onclick="creatObjectStore()">
</body>
</html>
版权所有,禁止匿名转载;禁止商业使用。