function insertAnObj(indexDbName,json,num) {
var openRequest = indexedDB.open(indexDbName,1);
openRequest.onerror = function(e) {//当创建数据库失败时候的回调
console.log("Database error: " + e.target.errorCode);
};
openRequest.onsuccess = function(event) {
console.log("Database created");
db = openRequest.result; //创建数据库成功时候,将结果给db,此时db就是当前数据库
//alert("this is :"+db);
//打开和userinfo相关的objectstore的事物
/*未打印*/
var Notprint = db.transaction("Notprint",'readwrite');
/*已打印快递单*/
var Already=db.transaction("Already",'readwrite');
/*已打印发货单*/
var invoices=db.transaction("invoices",'readwrite');
/*已录单*/
var Recorded=db.transaction("Recorded",'readwrite');
/*已发货*/
var Shipped=db.transaction("Shipped",'readwrite');
/*退款管理*/
var Refund=db.transaction("Refund",'readwrite');
switch (num){
case 0:
var number=Notprint;
var numb="Notprint"
break;
case 1:
var number=Already;
var numb="Already"
break;
case 2:
var number=invoices;
var numb="invoices"
break;
case 3:
var number=Recorded;
var numb="Recorded"
break;
case 4:
var number=Shipped;
var numb="Shipped"
break;
case 5:
var number=Refund;
var numb="Refund"
break;
}
var store=number.objectStore(numb);
for(var i=0;i<json.length;i++){
store.add(json[i]);//将对象添加至userinfo相关的objectstore中
}
};
openRequest.onupgradeneeded = function(event) {
var db = event.target.result;
//在第一次创建数据库的时候,就创建userinfo相关的objectstore,以供后面添加数据时候使用
if(!db.objectStoreNames.contains('Refund')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Refund',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
if(!db.objectStoreNames.contains('Shipped')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Shipped',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
if(!db.objectStoreNames.contains('Recorded')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Recorded',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
if(!db.objectStoreNames.contains('invoices')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('invoices',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
if(!db.objectStoreNames.contains('Already')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Already',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
if(!db.objectStoreNames.contains('Notprint')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Notprint',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}
}
}代码有些长, 就是我从后台拿过来的数据 传进来, 没有建索引的时候很正常 数据能正常存储进来,可是我一但建立了索引那么 if(!db.objectStoreNames.contains('Shipped')){
//keyPath:Javascript对象,对象必须有一属性作为键值
var store=db.createObjectStore('Shipped',{keyPath:"groupId"});
store.createIndex('buyerName','name',{unique:true});
}这个表里 包括索引里 都没有数据,调试也没有报错 ! 来大神拯救
添加回答
举报
0/150
提交
取消