html5本地存储
- 格式:doc
- 大小:22.79 KB
- 文档页数:9
本地数据库Demo用到的技术Html5,JavaScript,Jquery应用环境:Chrome浏览器根据Html5提供的LocalDatabase API,在浏览器里面建立数据库,然后下载服务器数据,保存到本地,以便需要的时候直接从本地查询数据库。
详细使用方法:创建数据库:var db = openDatabase('myData','1.0','test database',1024*1024);openDatabase()方法含有四个参数,第一个参数为数据库名,第二个参数为版本号,第三个参数为数据库的描述,第四个参数为数据库的大小(1024*1024表示1M大小)执行SQL的主要方法是executeSql()db.transaction(function(tx){tx.executeSql('CREATE TABLE IF NOT EXISTS MsgData(name TEXT,msg TEXT,time INTEGER)', []);})查询方法:transaction.executeSql(SQLquery,[],dataHandler,errorHandler);第一个参数是要执行的SQL语句,如如果其中含有未知的参数,用?代替;第二个参数是SQL语句需要用到的参数,即取代第一个参数中的?;第三个参数是查询结果的处理函数;第四个是错误处理函数;数据库查询结果的数据集对象有一个rows属性,其中保存了查询到的每条记录,可以用for循环来一次取出里面的数据:for(var i = 0; i < rows.length; i++){var temp = rows.item(i);}操作实例代码:// JavaScript Documentvar db, edit_record = 0, errormsg = [];errormsg[0] = "Local Database error:";errormsg[1] = "Failed to open the local database. There may not be enough space left in this domain's quota";errormsg[2] = "Can't open a database. Get a Browser that supports HTML 5 like Googe Chrome";errormsg[3] = "Unable to get list of pages from your database"; errormsg[4] = "Unable to update record.";errormsg[5] = "Unable to create new record";errormsg[6] = "Unable to delete record";errormsg[7] = "Unable to delete all records in table";var number=1;var SQLtxt = [];var inetstatus = "OFFLINE";function check_inet() {inetstatus = navigator.onLine ? "ONLINE" : "OFFLINE";document.addEventListener("offline", function() {inetstatus = navigator.onLine ? "ONLINE" : "OFFLINE"}, false);document.addEventListener("online", function() {inetstatus = navigator.onLine ? "ONLINE" : "OFFLINE"}, false)}function Error_DB(a, b) {var c = errormsg[a] + "\nDetails:" + b.message;alert(c)}/*** 打开数据库***/function Open_DB(b, d, a, c) {try {if (window.openDatabase) {db = openDatabase(b, d, a, c);if (!db)Error_DB(1, null)} elseError_DB(2, null)} catch (e) {Error_DB(0, e)}}/*** 建表***/function build_db(SQL) {if (db)db.transaction(function(a) {a.executeSql(SQL)})}/*** SQL执行* 建表,插入数据* SQL**/function execSQL(SQL){db.transaction(function(a) {for(var i=1;i<=30000;i++){SQL = "insert into T_ICD10_New ( id , ZYBM , FJBM , JBMC , BM , LB ) values"+"( "+i+" , 'A00"+i+"' , 'A"+i+"' , '神经病神经病神经病"+i+"' , '天才"+i+"' , 5 )";a.executeSql(SQL, [], function() {$(".right").html("<i>"+(number++)+"/30000</i><br/><br/>") ;//var t = document.getElementById('right');//t.scrollTop = t.scrollHeight;}, function() {$(".right").append(".") ;//var t = document.getElementById('right');//t.scrollTop = t.scrollHeight;})}})}/*** 查询列表* SQL**/function selectSQL(SQL){db.transaction(function(a) {a.executeSql(SQL, [], function(b, a) {showlist(a)}, function() {$(".right").append("<i>Select failure</i><br/><br/>") ;})})}/*** 页面显示列表***/function showlist(c){$(".right > table > tbody").empty();for ( var b = 0; b < c.rows.length; ++b) {var a = c.rows.item(b);$(".right > table").append("<tr>"+"<td>"+a["id"]+"</td>"+"<td>"+a["ZYBM"]+"</td>"+"<td>"+a["FJBM"]+"</td>"+"<td>"+a["JBMC"]+"</td>"+"<td>"+a["BM"]+"</td>"+"<td>"+a["LB"]+"</td>"+"</tr>");}}function selectTree(id){var SQL="select id , fatherid , category , categoryname , endflag from t_icd_category wherefatherid = " + id;db.transaction(function(a) {a.executeSql(SQL, [], function(b, a) {showTree(a , id);}, function() {$(".right").append("<i>Select failure</i><br/><br/>") ;})})}function showTree(c , id){for ( var b = 0; b < c.rows.length; ++b) {var a = c.rows.item(b);var html="<div class='tree' id='"+a['id']+"'>";if(!a['endflag'])html+="<span class='plus open'>+</span><span class='showlist open'>";elsehtml+="<span class='showlist endflag'>";html+=a["categoryname"]+"</span></div>";$(".left").find("#"+id).append(html);}}/*** 查询总页数**/function getCount(pid , keywordZYBM ,keywordJBMC){var SQL="select count(id) as count from T_ICD10_New";if(pid != 0 && pid != ""){SQL += " WHERE LB =" + pid ;if(keywordZYBM) SQL += " AND ZYBM LIKE '%"+keywordZYBM+"%'";if(keywordJBMC) SQL += " AND JBMC LIKE '%"+keywordJBMC+"%'";}else if(keywordZYBM){SQL += " WHERE ZYBM LIKE '%"+keywordZYBM+"%'";if(keywordJBMC) SQL += " AND JBMC LIKE '%"+keywordJBMC+"%'";}else if(keywordJBMC) SQL += " WHERE JBMC LIKE '%"+keywordJBMC+"%'";db.transaction(function(a) {a.executeSql(SQL, [], function(b,k) {var c=k.rows.item(0);$("#countPage").html(Math.ceil(c['count']/25));})})}/*function adddata() {db.transaction(function(a) {a.executeSql(SQLtxt[8], [], function(c, a) {var b = a.rows.item(0);if (b["TotCnt"] < 1)adddata_step2();get_list()}, function(b, a) {Error_DB(3, a)})})}function adddata_step2() {db.transaction(function(b) {var a = [ "Walter Moore", "727.827.9035" ];b.executeSql(SQLtxt[3], a, function() {}, function(b, a) {Error_DB(0, a)});a = [ "Betty Smith", "727.967.1111" ];b.executeSql(SQLtxt[3], a, function() {}, function(b, a) {Error_DB(0, a)})})}function get_list() {db.transaction(function(a) {a.executeSql(SQLtxt[4], [], function(b, a) {loadlist(a)}, function(b, a) {Error_DB(3, a)})})}function loadlist(c) {var d = "";for ( var b = 0; b < c.rows.length; ++b) {var a = c.rows.item(b);d += "<br /><span style='cursor:pointer' onclick='return edit_contact(\""+ a["ContactId"]+ '","'+ a["ContactName"]+ '","'+ a["ContactPhone"]+ "\")'>Edit</span> / <span style='cursor:pointer' onclick='return delete_contact("+ a["ContactId"]+ ")'>Delete</span> -- Name: "+ a["ContactName"] + "-Phone: " + a["ContactPhone"]}}function delete_contact(a) {db.transaction(function(b) {b.executeSql(SQLtxt[6], [ a ], get_list(), function(b, a) {Error_DB(6, a)})})}function delete_alldata() {db.transaction(function(a) {a.executeSql(SQLtxt[7], [], get_list(), function(b, a) {Error_DB(7, a)})})}function store_local() {if (edit_record > 0) {var a = [ document.getElementById("ContactName").value,document.getElementById("ContactPhone").value, edit_record ];db.transaction(function(b) {b.executeSql(SQLtxt[5], a, Update_successful(), function(b, a) {Error_DB(4, a)})})} else {var a = [ document.getElementById("ContactName").value,document.getElementById("ContactPhone").value ];db.transaction(function(b) {b.executeSql(SQLtxt[3], a, Update_successful(), function(b, a) {Error_DB(5, a)})})}return false}function Update_successful() {document.getElementById("ContactName").value = "";document.getElementById("ContactPhone").value = "";edit_record = 0;get_list()}function edit_contact(c, b, a) {document.getElementById("ContactName").value = b;document.getElementById("ContactPhone").value = a;edit_record = c;return false}function show_demo() {document.getElementById("demo").style.display = "";document.getElementById("DemoDesc").style.display = "none" }function page_load() {check_inet();Open_DB("WMooreDB", "1.0", "WMooreDB", 5e6);build_db();adddata()}document.onload = page_load()*/。