html5的indexedDB数据库操作实例

效果:



 代码:

StringUtil.js

 

//去除字符串中间空格 
String.prototype.Trim = function() { 
	return this.replace(/(^s*)|(s*$)/g, ""); 
} 
//去除字符串左侧空格 
String.prototype.LTrim = function() { 
	return this.replace(/(^s*)/g, ""); 
} 
//去除字符串右侧空格 
String.prototype.RTrim = function() { 
	return this.replace(/(s*$)/g, ""); 
}
//去除字符串中所有空格(包括中间空格,需要设置第2个参数为:g) 
function Trim(str,is_global){ 
	var result; 
	result = str.replace(/(^s+)|(s+$)/g,""); 
	if(is_global.toLowerCase()=="g") 
	result = result.replace(/s/g,""); 
	return result; 
} 

 indexedDB.js

window.onload = function(){
	if(!window.indexedDB){
		window.indexedDB = window.mozIndexedDB || window.webkitIndexedDB;
	}
	
	var db = null;
	var request = indexedDB.open("mydb");
	request.onupgradeneeded = function(e){
		//db = request.result;
		db = e.target.result;
		createObjectStore(db);
	}
	
	function createObjectStore(db){
		if(db.objectStoreNames.contains("customer")){
			db.deleteObjectStore("customer");
		}
		
		var objectStore = db.createObjectStore("customer",{keyPath:"id",autoIncrement:true});
		
		objectStore.createIndex("name","name",{unique:false});
		objectStore.createIndex("email","email",{unique:true});
		
		objectStore.add({name: "Tom", sex: "male", age: 34, email: "tom@facebok.org"});
		objectStore.add({name: "Jiny", sex: "female", age: 25, email: "jiny@home.org"});
		objectStore.add({name: "Liam", sex: "male", age: 23, email: "liam@163.com"});
	}
	
	request.onsuccess = function(e){
		db = e.target.result;
		if(!db.version=="1.0"){
			var request = db.setVersion("1.0");
			request.onsuccess = function(e){
				createObjectStore(db);
				showDataByCursor();
			}
			request.onerror = function(e){
				alert(e);
			}
		}else{
			showDataByCursor();
		}
	}
	
	function showDataByCursor(objectStore){
		if(!objectStore){
			var transaction = db.transaction(["customer"]);
			objectStore = transaction.objectStore("customer");
		}
		console.log("Store-Name : "+objectStore.name);
		console.log("Store-KeyPath : "+objectStore.keyPath);
		
		var request = objectS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值