1,链接数据库
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script> window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; window.IDBCursor=window.IDBCursor || window.webkitIDBCursor ||window.msIDBCursor; function connectDatabase(){ var dbName = "indexedDBtest"; var dbVersion = 1; var idb; var dbConnect = indexedDB.open(dbName,dbVersion); //打开数据库 dbConnect.onsuccess = function(e){ //是否链接成功,链接成功之后,返回信息 idb = e.target.result; alert("数据库链接成功") } dbConnect.onerror = function(e){ alert("数据库链接失败") } } </script> </head> <body> <input type="button" value="链接数据库" onclick="connectDatabase()"> </body> </html>
2,数据库版本更新
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script> window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; window.IDBCursor=window.IDBCursor || window.webkitIDBCursor ||window.msIDBCursor; function versionUpdata(){ var dbName = "indexedDBtest"; var dbVersion = 2; var idb; var dbConnect = indexedDB.open(dbName,dbVersion); dbConnect.onsuccess = function(e){ idb = e.target.result; alert("数据库链接成功"); } dbConnect.error = function(){ alert("数据库更新失败") } dbConnect.onupgradeneeded = function(e){ idb = e.target.result; var tx = e.target.transaction; var oldVersion = e.oldVersion; var newVersion = e.newVersion; alert("数据库更新成功"+oldVersion+"--"+newVersion); } } </script> </head> <body> <input type="button" value="链接数据库" onclick="versionUpdata()"> </body> </html>
时间: 2024-10-13 05:21:15