HTML5本地存儲(chǔ)之Database Storage應(yīng)用介紹_HTML5教程
推薦:html5指南-4.使用Geolocation實(shí)現(xiàn)定位功能今天我們要學(xué)習(xí)的是使用Geolocation實(shí)現(xiàn)定位功能。我們可以通過(guò)navigator.geolocation獲取Geolocation對(duì)象,感興趣的朋友可以了解下
在上一篇《HTML5本地存儲(chǔ)之Web Storage篇》中,簡(jiǎn)單介紹了如何利用localStorage實(shí)現(xiàn)本地存儲(chǔ);實(shí)際上,除了sessionStorage和localStorage外,HTML5還支持通過(guò)本地?cái)?shù)據(jù)庫(kù)進(jìn)行本地?cái)?shù)據(jù)存儲(chǔ),HTML5采用的是"SQLLite"這種文件型數(shù)據(jù)庫(kù),該數(shù)據(jù)庫(kù)多集中在嵌入式設(shè)備上,熟悉IOS/Android開(kāi)發(fā)的同學(xué),應(yīng)該對(duì)SQLLite數(shù)據(jù)庫(kù)比較熟悉。HTML5中的數(shù)據(jù)庫(kù)操作比較簡(jiǎn)單,主要有如下兩個(gè)函數(shù):
1、通過(guò)openDatabase方法創(chuàng)建一個(gè)訪問(wèn)數(shù)據(jù)庫(kù)的對(duì)象
復(fù)制代碼 代碼如下:hl5o.cn
var db = openDatabase(databasename,version,description,size)
該方法有四個(gè)參數(shù),作用分別為:
databasename:數(shù)據(jù)庫(kù)名;
version:數(shù)據(jù)庫(kù)版本號(hào),可不填;
description:數(shù)據(jù)庫(kù)描述;
size:給數(shù)據(jù)庫(kù)分配的空間大小;
2、使用第一步創(chuàng)建的數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象(如db)執(zhí)行transaction方法,用來(lái)執(zhí)行事務(wù)處理
復(fù)制代碼 代碼如下:hl5o.cn
db.transaction(function(tx)){
//執(zhí)行訪問(wèn)數(shù)據(jù)庫(kù)的語(yǔ)句
});
transaction方法使用一個(gè)回調(diào)函數(shù)作為參數(shù),在這個(gè)函數(shù)中,執(zhí)行訪問(wèn)數(shù)據(jù)庫(kù)的具體操作;
3、通過(guò)executeSql方法執(zhí)行查詢
復(fù)制代碼 代碼如下:hl5o.cn
tx.executeSql(sqlQuery,[value1,value2..],dataHandler,errorHandler)
executeSql方法有四個(gè)參數(shù),作用分別如下:
sqlQuery:需要具體執(zhí)行的sql語(yǔ)句,可以是create、select、update、delete;
[value1,value2..]:sql語(yǔ)句中所有使用到的參數(shù)的數(shù)組,在executeSql方法中,將sql語(yǔ)句中所要使用的參數(shù)先用“?”代替,然后依次將這些參數(shù)組成數(shù)組放在第二個(gè)參數(shù)中;
dataHandler:執(zhí)行成功是調(diào)用的回調(diào)函數(shù),通過(guò)該函數(shù)可以獲得查詢結(jié)果集;
errorHandler:執(zhí)行失敗時(shí)調(diào)用的回調(diào)函數(shù);
本文通過(guò)HTML5的數(shù)據(jù)庫(kù)支持,重新實(shí)現(xiàn)一遍上篇文章中的通訊錄管理,待實(shí)現(xiàn)功能如下:
可創(chuàng)建聯(lián)系人并保存到數(shù)據(jù)庫(kù)中,聯(lián)系人字段包括:姓名、手機(jī)號(hào)碼、公司、創(chuàng)建時(shí)間;
列出當(dāng)前已保存的所有聯(lián)系人信息;
可刪除特定聯(lián)系人信息;
同樣,先準(zhǔn)備一個(gè)HTML頁(yè)面,如下:
復(fù)制代碼 代碼如下:hl5o.cn
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>HTML5本地存儲(chǔ)之本地?cái)?shù)據(jù)庫(kù)篇</title>
<style>
.addDiv{
border: 2px dashed #ccc;
width:400px;
text-align:center;
}
</style>
</head>
<body onload="loadAll()">
<div class="addDiv">
<label for="user_name">姓名:</label>
<input type="text" id="user_name" name="user_name" class="text"/>
<br/>
<label for="mobilephone">手機(jī):</label>
<input type="text" id="mobilephone" name="mobilephone"/>
<br/>
<label for="mobilephone">公司:</label>
<input type="text" id="company" name="company"/>
<br/>
<input type="button" onclick="save()" value="新增記錄"/>
</div>
<br/>
<div id="list">
</div>
</body>
</html>
界面展現(xiàn)如下:

要實(shí)現(xiàn)創(chuàng)建新聯(lián)系人并存入數(shù)據(jù)庫(kù)功能,需要如下簡(jiǎn)單的JS代碼:
復(fù)制代碼 代碼如下:hl5o.cn
//打開(kāi)數(shù)據(jù)庫(kù)
var db = openDatabase('contactdb','','local database demo',204800);
//保存數(shù)據(jù)
function save(){
var user_name = document.getElementById("user_name").value;
var mobilephone = document.getElementById("mobilephone").value;
var company = document.getElementById("company").value;
//創(chuàng)建時(shí)間
var time = new Date().getTime();
db.transaction(function(tx){
tx.executeSql('insert into contact values(?,?,?,?)',[user_name,mobilephone,company,time],onSuccess,onError);
});
}
//sql語(yǔ)句執(zhí)行成功后執(zhí)行的回調(diào)函數(shù)
function onSuccess(tx,rs){
alert("操作成功");
loadAll();
}
//sql語(yǔ)句執(zhí)行失敗后執(zhí)行的回調(diào)函數(shù)
function onError(tx,error){
alert("操作失敗,失敗信息:"+ error.message);
}
要展現(xiàn)當(dāng)前所有已保存的聯(lián)系人列表,可通過(guò)如下JS代碼實(shí)現(xiàn):
復(fù)制代碼 代碼如下:hl5o.cn
//將所有存儲(chǔ)在sqlLite數(shù)據(jù)庫(kù)中的聯(lián)系人全部取出來(lái)
function loadAll(){
var list = document.getElementById("list");
db.transaction(function(tx){
//如果數(shù)據(jù)表不存在,則創(chuàng)建數(shù)據(jù)表
tx.executeSql('create table if not exists contact(name text,phone text,company text,createtime INTEGER)',[]);
//查詢所有聯(lián)系人記錄
tx.executeSql('select * from contact',[],function(tx,rs){
if(rs.rows.length>0){
var result = "<table>";
result += "<tr><th>序號(hào)</th><th>姓名</th><th>手機(jī)</th><th>公司</th><th>添加時(shí)間</th><th>操作</th></tr>";
for(var i=0;i<rs.rows.length;i++){
var row = rs.rows.item(i);
//轉(zhuǎn)換時(shí)間,并格式化輸出
var time = new Date();
time.setTime(row.createtime);
var timeStr = time.format("yyyy-MM-dd hh:mm:ss");
//拼裝一個(gè)表格的行節(jié)點(diǎn)
result += "<tr><td>"+(i+1)+"</td><td>"+row.name+"</td><td>"+row.phone+"</td><td>"+row.company+"</td><td>"+timeStr+"</td><td><input type='button' value='刪除' onclick='del("+row.phone+")'/></td></tr>";
}
list.innerHTML = result;
}else{
list.innerHTML = "目前數(shù)據(jù)為空,趕緊開(kāi)始加入聯(lián)系人吧";
}
});
});
}
其中,涉及到格式化時(shí)間的format函數(shù),可參考如下JS實(shí)現(xiàn):
復(fù)制代碼 代碼如下:hl5o.cn
Date.prototype.format = function(format)
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
最后,界面實(shí)現(xiàn)效果如下:
要實(shí)現(xiàn)具體某個(gè)聯(lián)系人,需執(zhí)行如下JS代碼:
復(fù)制代碼 代碼如下:hl5o.cn
//刪除聯(lián)系人信息
function del(phone){
db.transaction(function(tx){
//注意這里需要顯示的將傳入的參數(shù)phone轉(zhuǎn)變?yōu)樽址?lèi)型
tx.executeSql('delete from contact where phone=?',[String(phone)],onSuccess,onError);
});
}
如上截圖中的表格樣式,可參考如下CSS代碼:
復(fù)制代碼 代碼如下:hl5o.cn
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
}
td {
border-right: 1px solid #C9DAD7;
border-bottom: 1px solid #C9DAD7;
background: #fff;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
分享:html5中canvas學(xué)習(xí)筆記1-畫(huà)板的尺寸與實(shí)際顯示尺寸在canvas中當(dāng)在canvas上寫(xiě)width和height時(shí)為canvas的實(shí)際畫(huà)板大小,默認(rèn)情況下width為300px,height為150px,接下來(lái)詳細(xì)介紹,感興趣的朋友可以參考下
相關(guān)HTML5教程:
- 只要五步 就可以用HTML5/CSS3快速制作便簽貼特效(圖)
- 網(wǎng)易微博Web App用HTML5開(kāi)發(fā)的過(guò)程介紹
- HTML5 對(duì)各個(gè)標(biāo)簽的定義與規(guī)定:body的介紹
- 關(guān)于HTML5的安全問(wèn)題開(kāi)發(fā)人員需要牢記的
- 關(guān)于HTML5的22個(gè)初級(jí)技巧(圖文教程)
- 開(kāi)發(fā)人員所需要知道的HTML5性能分析面面觀
- HTML5 Web Database 數(shù)據(jù)庫(kù)的SQL語(yǔ)句的使用方法
- HTML5實(shí)踐-圖片設(shè)置成灰度圖
- HTML5安全介紹之內(nèi)容安全策略(CSP)簡(jiǎn)介
- HTML5 Web存儲(chǔ)方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲(chǔ)案例應(yīng)用
- Bootstrap 學(xué)習(xí)分享
- input元素的url類(lèi)型和email類(lèi)型簡(jiǎn)介
HTML5教程Rss訂閱Div+Css教程搜索
HTML5教程推薦
- html5指南-1.html5全局屬性(html5 global attributes)深入理解
- html5 canvas里繪制橢圓并保持線條粗細(xì)均勻的技巧
- Bootstrap 學(xué)習(xí)分享
- HTML5之SVG 2D入門(mén)12—SVG DOM及DOM操作介紹
- 純html5+css3下拉導(dǎo)航菜單實(shí)現(xiàn)代碼
- html5指南-4.使用Geolocation實(shí)現(xiàn)定位功能
- html5中canvas學(xué)習(xí)筆記2-判斷瀏覽器是否支持canvas
- HTML4和HTML5之間除了相似以外的10個(gè)主要不同
- HTML中fieldset標(biāo)簽概述及使用方法
- 網(wǎng)易微博Web App用HTML5開(kāi)發(fā)的過(guò)程介紹
- 相關(guān)鏈接:
- 教程說(shuō)明:
HTML5教程-HTML5本地存儲(chǔ)之Database Storage應(yīng)用介紹
。