您好,欢迎访问一九零五行业门户网

本地数据库是html5新特性吗

本地数据库是html5新特性。html5提供了一个浏览器端的数据库支持,允许开发者直接通js的api在浏览器端创建一个本地的数据库,而且支持标准的sql的crud操作,让离线的web应用更加方便的存储结构化的数据。
本教程操作环境:windows7系统、html5版、dell g3电脑。
虽然html5已经提供了功能强大的localstorage和sessionstorage,但是他们两个都只能提供存储简单数据结构的数据,对于复杂的web应用的数据却无能为力。逆天的是html5提供了一个浏览器端的数据库支持,允许我们直接通js的api在浏览器端创建一个本地的数据库,而且支持标准的sql的crud操作,让离线的web应用更加方便的存储结构化的数据。接下里介绍一下本地数据的相关api和用法。
操作本地数据库的最基本的步骤是:
第一步:opendatabase方法:创建一个访问数据库的对象。第二步:使用第一步创建的数据库访问对象来执行transaction方法,通过此方法可以设置一个开启事务成功的事件响应方法,在事件响应方法中可以执行sql.第三步:通过executesql方法执行查询,当然查询可以是:crud。接下来分别介绍一下相关的方法的参数和用法。
(1)opendatabase方法:
//demo:获取或者创建一个数据库,如果数据库不存在那么创建之var database = opendatabase("student", "1.0", "学生表", 1024 * 1024, function () { });
opendatabase方法打开一个已经存在的数据库,如果数据库不存在,它还可以创建数据库。几个参数意义分别是:
1,数据库名称。2,数据库的版本号,目前来说传个1.0就可以了,当然可以不填;3,对数据库的描述。4,设置分配的数据库的大小(单位是kb)。5,回调函数(可省略)。初次调用时创建数据库,以后就是建立连接了。(2)db.transaction方法可以设置一个回调函数,此函数可以接受一个参数就是我们开启的事务的对象。然后通过此对象可以进行执行sql脚本,跟下面的步骤可以结合起来。
(3)通过executesql方法执行查询。
ts.executesql(sqlquery,[value1,value2..],datahandler,errorhandler)
参数说明:
qlquery:需要具体执行的sql语句,可以是create、select、update、delete;value1,value2..]:sql语句中所有使用到的参数的数组,在executesql方法中,将s>语句中所要使用的参数先用“?”代替,然后依次将这些参数组成数组放在第二个参数中atahandler:执行成功是调用的回调函数,通过该函数可以获得查询结果集;4,errorhandler:执行失败时调用的回调函数;下面是一个综合的例子,可以看一下:
<head> <script src="scripts/jquery-1.5.1.js" type="text/javascript"></script> <script type="text/javascript"> function initdatabase() { var db = getcurrentdb();//初始化数据库 if(!db) {alert("您的浏览器不支持html5本地数据库");return;} db.transaction(function (trans) {//启动一个事务,并设置回调函数 //执行创建表的sql脚本 trans.executesql("create table if not exists demo(uname text null,title text null,words text null)", [], function (trans, result) { }, function (trans, message) {//消息的回调函数alert(message);}); }, function (trans, result) { }, function (trans, message) { }); } $(function () {//页面加载完成后绑定页面按钮的点击事件 initdatabase(); $("#btnsave").click(function () { var txtname = $("#txtname").val(); var txttitle = $("#txttitle").val(); var txtwords = $("#txtwords").val(); var db = getcurrentdb(); //执行sql脚本,插入数据 db.transaction(function (trans) { trans.executesql("insert into demo(uname,title,words) values(?,?,?) ", [txtname, txttitle, txtwords], function (ts, data) { }, function (ts, message) { alert(message); }); }); showallthedata(); }); }); function getcurrentdb() { //打开数据库,或者直接连接数据库参数:数据库名称,版本,概述,大小 //如果数据库不存在那么创建之 var db = opendatabase("mydb", "1.0", "it's to save demo data!", 1024 * 1024); ; return db; } //显示所有数据库中的数据到页面上去 function showallthedata() { $("#tbldata").empty(); var db = getcurrentdb(); db.transaction(function (trans) { trans.executesql("select * from demo ", [], function (ts, data) { if (data) { for (var i = 0; i < data.rows.length; i++) { appenddatatotable(data.rows.item(i));//获取某行数据的json对象 } } }, function (ts, message) {alert(message);var tst = message;}); }); } function appenddatatotable(data) {//将数据展示到表格里面 //uname,title,words var txtname = data.uname; var txttitle = data.title; var words = data.words; var strhtml = ""; strhtml += "<tr>"; strhtml += "<td>"+txtname+"</td>"; strhtml += "<td>" + txttitle + "</td>"; strhtml += "<td>" + words + "</td>"; strhtml += "</tr>"; $("#tbldata").append(strhtml); } </script></head> <body> <table> <tr> <td>用户名:</td> <td><input type="text" name="txtname" id="txtname" required/></td> </tr> <tr> <td>标题:</td> <td><input type="text" name="txttitle" id="txttitle" required/></td> </tr> <tr> <td>留言:</td> <td><input type="text" name="txtwords" id="txtwords" required/></td> </tr> </table> <input type="button" value="保存" id="btnsave"/> <hr/> <input type="button" value="展示所哟数据" onclick="showallthedata();"/> <table id="tbldata"> </table> </body></html>
执行的效果如图:
相关推荐:《html视频教程》
以上就是本地数据库是html5新特性吗的详细内容。
其它类似信息

推荐信息