更新indexeddb中的单个字段

时间:2017-01-09 16:48:28

标签: indexeddb

以下工作代码用于设置我的问题:

myRequest = indexedDB.deleteDatabase('myDatabase')
myRequest.onsuccess	= function() {
	var myRequest
	
	myRequest = indexedDB.open('myDatabase')
	myRequest.onupgradeneeded = function(response) {
		var myDatabase
			,myObjectStore
		
		myDatabase = response.target.result
		myObjectStore = myDatabase.createObjectStore('myData',{autoIncrement:true})
	}
	myRequest.onerror = function(response) {
		debugger
	}
	myRequest.onsuccess = function(response) {
		var myTransaction
			,myObjectStore
			,myRequest
			,obj = {}
		window.myDatabase = response.target.result
		myTransaction = myDatabase.transaction(['myData'],'readwrite')
		myObjectStore = myTransaction.objectStore('myData')
		obj.field1 = 'a'
		obj.field2 = 'b'
		myObjectStore.add(obj)
	}
}

问:我怎么说相当于:

update myData set field2='c' where key=1

我是否必须在.put语句中提供field1和field2?

2 个答案:

答案 0 :(得分:3)

是的,您必须放置完整的对象,例如

myObjectStore.get(1).onsuccess = function(e) {
  var obj = e.target.result;
  obj.field2 = 'c';
  myObjectStore.put(obj, 1);
};

答案 1 :(得分:0)

myRequest = indexedDB.deleteDatabase('myDatabase')
myRequest.onsuccess	= function() {
	var myRequest
	
	myRequest = indexedDB.open('myDatabase')
	myRequest.onupgradeneeded = function(response) {
		var myDatabase
			,myObjectStore
		
		myDatabase = response.target.result
		myObjectStore = myDatabase.createObjectStore('myData',{autoIncrement:true})
	}
	myRequest.onerror = function(response) {
		debugger
	}
	myRequest.onsuccess = function(response) {
		var myTransaction
			,myObjectStore
			,myRequest
			,obj = {}
		window.myDatabase = response.target.result
		myTransaction = myDatabase.transaction(['myData'],'readwrite')
		myObjectStore = myTransaction.objectStore('myData')
		obj.field1 = 'a'
		obj.field2 = 'b'
		myObjectStore.add(obj)
	}
}

myRequest = indexedDB.deleteDatabase('myDatabase')
myRequest.onsuccess	= function() {
	var myRequest
	
	myRequest = indexedDB.open('myDatabase')
	myRequest.onupgradeneeded = function(response) {
		var myDatabase
			,myObjectStore
		
		myDatabase = response.target.result
		myObjectStore = myDatabase.createObjectStore('myData',{autoIncrement:true})
	}
	myRequest.onerror = function(response) {
		debugger
	}
	myRequest.onsuccess = function(response) {
		var myTransaction
			,myObjectStore
			,myRequest
			,obj = {}
		window.myDatabase = response.target.result
		myTransaction = myDatabase.transaction(['myData'],'readwrite')
		myObjectStore = myTransaction.objectStore('myData')
		obj.field1 = 'a'
		obj.field2 = 'b'
		myObjectStore.add(obj)
	}
}