<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<
html
>
<
head
>
<
meta
http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
<
title
>创建索引
</
title
>
<
script
>
var
myDB={
name:
"helloindexDB",
version:
1,
db:
null
}
function
openDB(
name,
version){
var
version =
version ||
1;
var
request=
widow.
indexedDB.
open(
name,
version);
request.
onerror=
function(
e){
}
request.
onsuccess=
function(
e){
myDB.
db=
e.
target.
result;
}
//以下是 对 数据表 进行操作
request.
onupgradeneeded=
function(
e){
var
db=
e.
target.
result;
//如果没有 student 表
if(!
db.
objectStoreNames.
contains(
"students"))
{
//创建student表,并设置id为key
var
story=
db.
createObjectStore(
"students",{
keyPath:
"id"});
//创建字段
story.
createIndex(
"nameIndex",
"name",{
unique:
true} );
story.
createIndex(
"ageIndex",
"age",{
unique:
false});
}
}
}
var
students=
[
{
id:
101,
name:
"aa",
age:
10
},
{
id:
102,
name:
"bb",
age:
11
},
{
id:
103,
name:
"cc",
age:
12
}
]
function
addData(
db,
storeName){
var
transaction=
db.
transaction(
storeName,
"readwrite");
//读取权限
var
store=
transaction.
objectStore(
storeName);
for(
var
i=
0;
i<
students.
length;
i++)
{
store.
add(
students[
i]);
}
}
//执行操作
openDB(
myDB.
name,
myDB.
version);
setTimeout(
function(){
addData(
myDB.
db,
"students");
},
1000);
</
script
>
</
head
>
<
body
>
</
body
>
</
html
>