ale*_*use 5 javascript indexeddb
我在这里有创建 indexedDB 的代码:
\n\nfunction create_db() {\n var indexedDB = window.indexedDB || window.webkitIndexedDB || window.msIndexedDB;\n var request = indexedDB.open(\xe2\x80\x9cphotos\xe2\x80\x9d, 2);\n\n request.onupgradeneeded = function(event) {\n var db = event.target.result;\n\n // Create photo db\n var photo_store = db.createObjectStore("photos", {keyPath: "photo_id"});\n var photo_id_index = photo_store.createIndex("by_photo_id", "photo_id", {unique: true});\n var dest_id_index = photo_store.createIndex("by_destination_id", "destination_id");\n\n console.log(\xe2\x80\x9cstore created\xe2\x80\x9d);\n };\n\n request.onsuccess = function(event) {\n console.log(\xe2\x80\x9cstore opened\xe2\x80\x9d);\n };\n\n request.onerror = function(event) {\n console.log("error: " + event);\n };\nRun Code Online (Sandbox Code Playgroud)\n\n}
\n\n我的删除条目的代码:
\n\n function remove_photos = function (destination_id, db) {\nvar transaction = db.transaction("photos", "readwrite");\nvar store = transaction.objectStore("photos");\nvar index = store.index("by_destination_id");\nvar request = index.openCursor(IDBKeyRange.only(destination_id));\n\nrequest.onsuccess = function() {\n var cursor = request.result;\n\n if (cursor) {\n cursor.delete();\n cursor.continue();\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n}
\n\n如何使用by_destination_id索引删除记录,以便删除具有给定destination_id(整数)的所有记录?
\n\n谢谢你的帮助。
\n我找到了问题的解决方案,IDBKeyRange.only 函数不喜欢整数,它需要是字符串,因此将此行替换为:
var request = index.openCursor(IDBKeyRange.only(destination_id.toString()));
Run Code Online (Sandbox Code Playgroud)
让代码发挥作用。